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

implementing Interfaces #163

Closed
nikeee opened this issue Oct 15, 2022 · 2 comments · Fixed by #167
Closed

implementing Interfaces #163

nikeee opened this issue Oct 15, 2022 · 2 comments · Fixed by #167

Comments

@nikeee
Copy link
Contributor

nikeee commented Oct 15, 2022

The docs state:

#[implements(ce)] - Implements the given interface on the class. Can be used multiple times. ce must be a valid Rust expression when it is called inside the #[php_module] function.

How do I use this to implement an interface provided by the standard library of PHP? For example, I want to implement ArrayAccess on my own class.

My current approach is this:

#[php_class]
#[implements(ext_php_rs::zend::class::ClassEntry::try_find("ArrayAccess").unwrap())]
pub struct Test {
}

But the class type seems to be private:

error[E0603]: module `class` is private
   --> src/lib.rs:205:1

If I get this to work, how do I actually implement the methods? Does having the appropriate method names suffice (putting renaming of methods aside)?

If this has a solution, I'd love to contribute this to the docs. I really like this library!

nikeee added a commit to nikeee/ext-php-rs that referenced this issue Oct 15, 2022
The zend API also has some standard interfaces it exposes:
https://github.com/php/php-src/blob/c8c09b4aaee7ac447828564c6267e62eb304135b/Zend/zend_interfaces.h#L27-L33
```
extern ZEND_API zend_class_entry *zend_ce_traversable;
extern ZEND_API zend_class_entry *zend_ce_aggregate;
extern ZEND_API zend_class_entry *zend_ce_iterator;
extern ZEND_API zend_class_entry *zend_ce_arrayaccess;
extern ZEND_API zend_class_entry *zend_ce_serializable;
extern ZEND_API zend_class_entry *zend_ce_countable;
extern ZEND_API zend_class_entry *zend_ce_stringable;
```

This surfaced in davidcole1340#163 and should make it possible to implement these interfaces.
@davidcole1340
Copy link
Owner

I don't think I have actually tried to implement an interface, so I will give it a go and report back. To answer your question regarding ClassEntry, it is re-exported via ext_php_rs::zend::ClassEntry::try_find (https://docs.rs/ext-php-rs/0.7.4/ext_php_rs/zend/type.ClassEntry.html).

davidcole1340 pushed a commit to nikeee/ext-php-rs that referenced this issue Oct 16, 2022
The zend API also has some standard interfaces it exposes:
https://github.com/php/php-src/blob/c8c09b4aaee7ac447828564c6267e62eb304135b/Zend/zend_interfaces.h#L27-L33
```
extern ZEND_API zend_class_entry *zend_ce_traversable;
extern ZEND_API zend_class_entry *zend_ce_aggregate;
extern ZEND_API zend_class_entry *zend_ce_iterator;
extern ZEND_API zend_class_entry *zend_ce_arrayaccess;
extern ZEND_API zend_class_entry *zend_ce_serializable;
extern ZEND_API zend_class_entry *zend_ce_countable;
extern ZEND_API zend_class_entry *zend_ce_stringable;
```

This surfaced in davidcole1340#163 and should make it possible to implement these interfaces.
davidcole1340 added a commit that referenced this issue Oct 16, 2022
* Add some standard zend interfaces

The zend API also has some standard interfaces it exposes:
https://github.com/php/php-src/blob/c8c09b4aaee7ac447828564c6267e62eb304135b/Zend/zend_interfaces.h#L27-L33
```
extern ZEND_API zend_class_entry *zend_ce_traversable;
extern ZEND_API zend_class_entry *zend_ce_aggregate;
extern ZEND_API zend_class_entry *zend_ce_iterator;
extern ZEND_API zend_class_entry *zend_ce_arrayaccess;
extern ZEND_API zend_class_entry *zend_ce_serializable;
extern ZEND_API zend_class_entry *zend_ce_countable;
extern ZEND_API zend_class_entry *zend_ce_stringable;
```

This surfaced in #163 and should make it possible to implement these interfaces.

* Add some links to the php documentation

* update docs.rs bindings

Co-authored-by: David Cole <david.cole1340@gmail.com>
@nikeee
Copy link
Contributor Author

nikeee commented Oct 19, 2022

I tried adding am example in #167. Do the docs fail due to the version with the interface class entries not being released?

@nikeee nikeee changed the title Correctly implementing Interfaces implementing Interfaces Oct 22, 2022
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