Skip to content
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

How to init_object on self using new class binding method? #176

Closed
brettmc opened this issue Feb 19, 2025 · 3 comments · Fixed by #177
Closed

How to init_object on self using new class binding method? #176

brettmc opened this issue Feb 19, 2025 · 3 comments · Fixed by #177

Comments

@brettmc
Copy link
Contributor

brettmc commented Feb 19, 2025

I was testing out master branch and updating some code to use the changes from #175
It's not clear how I can create a class method (static or not) to create a new instance of self using this new approach? I didn't see any of the updated examples doing this.

For example, if I wanted to implement

$a = new A();
$b = $a->copy();
//or
$c = A::newInstance();

how do I get a reference to the class from my add_method or add_static_method so I can call init_object() on it?

I thought something like this might work:

pub fn make_a_class() -> ClassEntity<()> {
    let mut class =
        ClassEntity::<()>::new_with_default_state_constructor(A_CLASS_NAME);

    class.add_static_method("newInstance", Visibility::Public, |_| {
        let mut object = class.init_object()?;
        Ok::<_, phper::Error>(object)
    });
    class
}

but I get an error that init_object method is not found on ClassEntity...

@jmjoy
Copy link
Member

jmjoy commented Feb 19, 2025

Sorry, I didn't think of this situation. Let me think about it again. If it doesn't work, I will roll back #175

@jmjoy
Copy link
Member

jmjoy commented Feb 19, 2025

I think we can make this method public. What do you think?

phper/phper/src/classes.rs

Lines 693 to 697 in e90afbc

#[inline]
pub(crate) fn bind_class(&self) -> StateClass<T> {
self.bind_class.clone()
}
}

@brettmc
Copy link
Contributor Author

brettmc commented Feb 19, 2025

I think we can make this method public. What do you think?

If it works, I will be happy :)
If it works, I can write a test for it, and try to update some documentation too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants