CopernicaMarketingSoftware / PHP-CPP

Library to build PHP extensions with C++
http://www.php-cpp.com/
Apache License 2.0
1.43k stars 333 forks source link

How to implement standard PHP interfaces like \Traversable #358

Open sandrokeil opened 6 years ago

sandrokeil commented 6 years ago

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?

mvdwerve commented 6 years ago

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 commented 1 year ago

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.