-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Constructors with subclasses #138
Comments
Indeed, this is not working as of version 0.8.1: #[php_class]
pub struct Greeter(String);
#[php_impl]
impl Greeter {
pub fn __construct(who: String) -> Self {
Self(who)
}
pub fn greet(&self) -> String {
format!("Hello, {}!", self.0)
}
} <?php
var_dump((new Greeter('world'))->greet()); // => "Hello, world!"
$example = new class("it doesn't work") extends Greeter
{
public function greet(): string
{
return "Hello, it works!";
}
};
var_dump($example->greet()); // => "Hello, it doesn't work!" |
@joelwurtz I wonder if you've run into this issue / found a solution to this issue? |
or maybe @danog ! |
I think the issue is around here: https://github.com/davidcole1340/ext-php-rs/blob/dddc07f587cd5d40f3b3ff64b9a59fba8299d953/src/builders/class.rs#L164C58-L168 where PHP passes a reference to the |
@joehoyle Yes that's the issue. The Then this pointer should be passed to |
@ju1ius ok cool, I had a quick go at trying that, but then ran into panics in the |
Yeah, I tried to fix it sometime ago but ran into so much fundamental issues that I ended up writing my own library from scratch. 🤣 |
@ju1ius ah I see! Any chance that code is open sourced? |
Ok I think I got this working as it happens in #277. I wasn't that familiar with how the Class EntryIn this library, when a struct is marked as
Classes are registered in PHP with the ClassBuilder::new("MyClass").object_override::<MyClass>().build();
The That's essentially everythign that happens to register the class with the PHP runtime ahead of time. All other things are invoked once the class is called / instantiated from PHP. InstantiationWhen PHP user-land code instantiates the object with So, when the
So, the A reference to the Instantiation of the Rust struct happens when the Method CallingWe haven't talked about how method calling is brided in to Rust. When the PHP class is registered via the The Inheritance and Sub-ClassingThoug this isn't currently supported here's what likely needs to happen to make it work. In PHP inheritance this is only a single Because the Sub-class will inherit the |
It is planned yes, but no clear ETA. Probably sometime after the PHP 8.3 release. |
I'm not sure it's possible for classes via the php_impl macro to be subclassed, as the constructor will return
Self
, instead of the subclassed class. I might be misunderstanding how subclassing (from user land PHP) should be working though!The text was updated successfully, but these errors were encountered: