Skip to content

Fix GH-9271: Conflicts between interface constants and trait constants should trigger fatal error #9274

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Zend/tests/traits/constant_022.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
Conflicts between trait constants and interface constants trigger fatal errors
--FILE--
<?php

trait MyTrait {
const A = 1;
}
interface I {
const A = 2;
}

class D implements I {
use MyTrait;
}

echo D::A;
--EXPECTF--
Fatal error: I and MyTrait define the same constant (A) in the composition of D. However, the definition differs and is considered incompatible. Class was composed in %s on line %d
11 changes: 8 additions & 3 deletions Zend/zend_inheritance.c
Original file line number Diff line number Diff line change
Expand Up @@ -2256,7 +2256,7 @@ static bool do_trait_constant_check(zend_class_entry *ce, zend_class_constant *t
}
/* }}} */

static void zend_do_traits_constant_binding(zend_class_entry *ce, zend_class_entry **traits) /* {{{ */
static void zend_do_bind_trait_constants(zend_class_entry *ce, zend_class_entry **traits) /* {{{ */
{
size_t i;

Expand Down Expand Up @@ -2426,9 +2426,10 @@ static void zend_do_bind_traits(zend_class_entry *ce, zend_class_entry **traits)
efree(exclude_tables);
}

/* then flatten the constants and properties into it, to, mostly to notify developer about problems */
zend_do_traits_constant_binding(ce, traits);
/* then flatten the properties into it, to, mostly to notify developer about problems */
zend_do_traits_property_binding(ce, traits);

/* trait constants are flattened after inheriting interfaces to handle conflicts with constants from interfaces */
}
/* }}} */

Expand Down Expand Up @@ -2983,6 +2984,10 @@ ZEND_API zend_class_entry *zend_do_link_class(zend_class_entry *ce, zend_string
} else if (parent && parent->num_interfaces) {
zend_do_inherit_interfaces(ce, parent);
}
if (ce->num_traits) {
/* trait constants are bound after processing interfaces to handle conflicts with constants from interfaces */
zend_do_bind_trait_constants(ce, traits_and_interfaces);
}
if (!(ce->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT))
&& (ce->ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS))
) {
Expand Down