Skip to content

Commit 3d791e0

Browse files
for php 8 implementing just Traverable leads to an error, hence we implement IteratorAggregate
1 parent c2b36b9 commit 3d791e0

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

zend/classimpl.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,9 +1538,26 @@ zend_class_entry *ClassImpl::initialize(ClassBase *base, const std::string &pref
15381538
else std::cerr << "Derived class " << name() << " is initialized before base class " << interface->name() << ": interface is ignored" << std::endl;
15391539
}
15401540

1541-
// we may have to expose the Traversable or Serializable interfaces
1542-
if (_base->traversable()) zend_class_implements(_entry, 1, zend_ce_traversable);
1543-
if (_base->serializable()) zend_class_implements(_entry, 1, zend_ce_serializable);
1541+
// we may have to expose the Traversable
1542+
if (_base->traversable())
1543+
{
1544+
#if PHP_VERSION_ID < 80000
1545+
// up to php 7.x we can implement just the Traversable method (strictly speaking, it does not seem
1546+
// to be a user-space concern whether a class is traversable because of "iterator" or "iteratoraggregate"
1547+
zend_class_implements(_entry, 1, zend_ce_traversable);
1548+
#else
1549+
// from php 8.0 an error occurs if a class just implements traversable, without being an explicit
1550+
// subclass of Iterator or IteratorAggregate -- so we implement the iteraroraggregate instead
1551+
zend_class_implements(_entry, 1, zend_ce_aggregate);
1552+
#endif
1553+
}
1554+
1555+
// check if the Serializable interface
1556+
if (_base->serializable())
1557+
{
1558+
// add the Seriablable interface
1559+
zend_class_implements(_entry, 1, zend_ce_serializable);
1560+
}
15441561

15451562
// instal the right modifier (to make the class an interface, abstract class, etc)
15461563
_entry->ce_flags |= uint64_t(_type);

0 commit comments

Comments
 (0)