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 implement standard PHP interfaces like \Traversable #358

Open
sandrokeil opened this issue Nov 22, 2017 · 2 comments
Open

How to implement standard PHP interfaces like \Traversable #358

sandrokeil opened this issue Nov 22, 2017 · 2 comments

Comments

@sandrokeil
Copy link

We have implemented a traversable class like

    class Cursor : public Php::Base, public Php::Traversable
    {
    }

but in PHP the instanceOf \Traversable check will fail. If I use reflection to get the implemented interfaces, nothing is implemented. The extension export with added implements("Traversable") doesn't work.

Is there anything I missed? How can I let PHP know, that the Cursor class implemente the PHP native interface Traversable to use PHP classes like IteratorIterator?

@sandrokeil sandrokeil changed the title How to get "instanceOf \Traversable" working How to implement standard PHP interfaces like \Traversable Nov 23, 2017
@mvdwerve
Copy link
Contributor

Did you ever actually solve this? This should work, but I'll check it out in a bit to make sure that it does.

@betrixed
Copy link

betrixed commented May 15, 2023

This is here because the maintained release repository fork by FastDebug - Carlos,has no issues reporting.

Issue is that Php::Traversible code as documented online will not work as advertised in later versions of PHP. This likely happened for 7.3 and later.

On 8.1 for instance, the "Map" example as found in the online documentation, or similar, will not work.

On using the example code with minimal fixup for compile and link , PHP 8.1 reports -
"PHP Fatal error: Class Map must implement interface Traversable as part of either Iterator or IteratorAggregate in Unknown on line 0". Similar for code that did work with PHP 7.2.

IteratorAggregate demands that a real PHP class be returned with public "getIterator() method.
Such a class needs to implement public IteratorAggregate,

The solution could be to make a class that declares itself as implementing the IteratorAggregate interface, and return the iterator object that way.

That's another thing - this and perhaps a few other online examples don't have source code in the repository "Examples". Which should be also good for basic test cases. There are not enough examples and test cases.

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

No branches or pull requests

3 participants