Skip to content

Implement delyed early binding for classes without parents #11227

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Zend/tests/delayed_early_binding_redeclaration-1.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
class Bar extends Foo {}
2 changes: 2 additions & 0 deletions Zend/tests/delayed_early_binding_redeclaration-2.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
class Bar extends Foo {}
13 changes: 13 additions & 0 deletions Zend/tests/delayed_early_binding_redeclaration.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--TEST--
Delayed early binding throws class redeclaration error
--EXTENSIONS--
opcache
--FILE--
<?php
class Foo {}
include __DIR__ . '/delayed_early_binding_redeclaration-1.inc';
include __DIR__ . '/delayed_early_binding_redeclaration-2.inc';
var_dump(class_exists(Bar::class));
?>
--EXPECTF--
Fatal error: Cannot declare class Bar, because the name is already in use in %sdelayed_early_binding_redeclaration-2.inc on line %d
2 changes: 1 addition & 1 deletion Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -8098,7 +8098,7 @@ static void zend_compile_class_decl(znode *result, zend_ast *ast, bool toplevel)
zend_add_literal_string(&key);

opline->opcode = ZEND_DECLARE_CLASS;
if (extends_ast && toplevel
if (toplevel
&& (CG(compiler_options) & ZEND_COMPILE_DELAYED_BINDING)
/* We currently don't early-bind classes that implement interfaces or use traits */
&& !ce->num_interfaces && !ce->num_traits
Expand Down
12 changes: 10 additions & 2 deletions Zend/zend_inheritance.c
Original file line number Diff line number Diff line change
Expand Up @@ -3184,6 +3184,10 @@ ZEND_API zend_class_entry *zend_do_link_class(zend_class_entry *ce, zend_string
/* Check whether early binding is prevented due to unresolved types in inheritance checks. */
static inheritance_status zend_can_early_bind(zend_class_entry *ce, zend_class_entry *parent_ce) /* {{{ */
{
if (parent_ce == NULL) {
return INHERITANCE_SUCCESS;
}

zend_string *key;
zend_function *parent_func;
zend_property_info *parent_info;
Expand Down Expand Up @@ -3278,7 +3282,9 @@ ZEND_API zend_class_entry *zend_try_early_bind(zend_class_entry *ce, zend_class_
zend_class_entry *orig_linking_class;
uint32_t is_cacheable = ce->ce_flags & ZEND_ACC_IMMUTABLE;

UPDATE_IS_CACHEABLE(parent_ce);
if (parent_ce) {
UPDATE_IS_CACHEABLE(parent_ce);
}
if (is_cacheable) {
if (zend_inheritance_cache_get && zend_inheritance_cache_add) {
zend_class_entry *ret = zend_inheritance_cache_get(ce, parent_ce, NULL);
Expand Down Expand Up @@ -3321,7 +3327,9 @@ ZEND_API zend_class_entry *zend_try_early_bind(zend_class_entry *ce, zend_class_
zend_begin_record_errors();
}

zend_do_inheritance_ex(ce, parent_ce, status == INHERITANCE_SUCCESS);
if (parent_ce) {
zend_do_inheritance_ex(ce, parent_ce, status == INHERITANCE_SUCCESS);
}
if (parent_ce && parent_ce->num_interfaces) {
zend_do_inherit_interfaces(ce, parent_ce);
}
Expand Down
4 changes: 2 additions & 2 deletions Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -7791,7 +7791,7 @@ ZEND_VM_HANDLER(144, ZEND_DECLARE_CLASS, CONST, ANY)
ZEND_VM_NEXT_OPCODE_CHECK_EXCEPTION();
}

ZEND_VM_HANDLER(145, ZEND_DECLARE_CLASS_DELAYED, CONST, CONST)
ZEND_VM_HANDLER(145, ZEND_DECLARE_CLASS_DELAYED, CONST, CONST|UNUSED)
{
USE_OPLINE

Expand All @@ -7801,7 +7801,7 @@ ZEND_VM_HANDLER(145, ZEND_DECLARE_CLASS_DELAYED, CONST, CONST)
zval *zv = zend_hash_find_known_hash(EG(class_table), Z_STR_P(lcname + 1));
if (zv) {
SAVE_OPLINE();
ce = zend_bind_class_in_slot(zv, lcname, Z_STR_P(RT_CONSTANT(opline, opline->op2)));
ce = zend_bind_class_in_slot(zv, lcname, OP2_TYPE == IS_CONST ? Z_STR_P(RT_CONSTANT(opline, opline->op2)) : NULL);
if (!ce) {
HANDLE_EXCEPTION();
}
Expand Down
Loading