[php-src] PR #21380: Fix: internal interfaces should not prevent class/enum early binding
| From: | AJenbo | Date: | Sat, 07 Mar 2026 19:19:16 +0000 |
| Subject: | [php-src] PR #21380: Fix: internal interfaces should not prevent class/enum early binding | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-35128@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/21380
Author: AJenbo
## Problem
Implementing an internal interface prevents the compiler from hoisting (early binding) a class or
enum, breaking forward references that would otherwise work:
```php
// Works
class B extends A {}
class A {}
// Fatal error: Class "A" not found
class B extends A {}
class A {
public function __toString(): string { return ''; }
}
// Same problem with explicit Stringable
class B extends A {}
abstract class A implements Stringable {}
// Same problem with other internal interfaces
class B extends A {}
class A implements Countable {
public function count(): int { return 0; }
}
// Same problem with enums (implicit UnitEnum)
echo Suit::Hearts->name;
enum Suit { case Hearts; }
```
Previously reported as #7873, #8323, and #19729, all closed as won't fix.
## Root cause
The early binding guard in
zend_compile_class_decl() checks
!ce->num_interfaces and skips hoisting when any interfaces are present:
```c
/* We currently don't early-bind classes that implement interfaces or use traits */
if (!ce->num_interfaces && !ce->num_traits && ...) {
```
This makes sense for userland interfaces that may not exist yet at compile time, but internal
interfaces like Stringable, Countable, ArrayAccess, Iterator, IteratorAggregate, Traversable,
UnitEnum, and BackedEnum are registered during engine startup and are always available.
The most common triggers:
- __toString(): add_stringable_interface() implicitly adds Stringable
during compilation
- Enums: zend_enum_add_interfaces() implicitly adds UnitEnum (and BackedEnum)
- Any explicit implements of a built-in interface
## Fix
**Zend/zend_compile.c:**
- New zend_can_early_bind_interfaces() checks whether all interfaces on a class are
internal engine interfaces. Serializable is excluded because its
interface_gets_implemented callback calls zend_error(E_DEPRECATED) which
triggers the user error handler, and early binding would change when that fires relative to
set_error_handler() calls.
- zend_inheritance_check_override() moved after interface resolution so
#[\Override] works correctly with interface methods.
- Enums get zend_enum_register_funcs() (registers cases(),
from(), tryFrom()) and zend_verify_enum() in the early
binding path, matching what zend_do_link_class() does.
**Zend/zend_inheritance.c:**
- New zend_early_bind_resolve_internal_interfaces() for the no-parent early binding
path. Uses zend_do_implement_interfaces() (same function
zend_do_link_class() uses) for correct deduplication of overlapping interface
hierarchies (e.g. a class implementing both RecursiveIterator and Iterator).
- zend_try_early_bind() extended to handle classes that have their own unresolved
interfaces when early binding with a parent, combining them with parent interfaces via
zend_do_implement_interfaces().
## Tests
New test Zend/tests/early_binding_internal_interfaces.phpt covers:
- Implicit Stringable (from __toString())
- Explicit implements Stringable
- Countable, ArrayAccess, IteratorAggregate
- Multiple internal interfaces combined
- Abstract parent with Stringable, child with __toString()
- Both parent and child having __toString()
- String casting through inheritance
- Unit enum hoisting with cases()
- Backed enum hoisting with from() and tryFrom()
Full test suite, zero regressions:
- Zend: 5302 tests (5160 pass, 141 skip, 1 xfail)
- Opcache: 907 tests (874 pass, 32 skip, 1 xfail)
- SPL: 781 tests (774 pass, 7 skip)
- Enum: 151 tests (142 pass, 9 skip)
Closes #7873
Closes #8323
Closes #19729