Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fixed bug #64235 (Insteadof not work for class method in 5.4.11) #284

Closed
wants to merge 1 commit into from

3 participants

@reeze

After the traits refactor, it "fixed" the traits precedence which could use class but not trait that was not intend from the design and manual.

this PR fixes the BC, and the same time deprecate the behavior.

This is a 5.4 only patch.

@laruence
Owner

actually, I was looking into this, I have thought the similar fix of you, but
unfortunatly, it is wrong, thinking of this:

<?php
class TestParentClass
{
public function method()
{
print_r('Parent method');
print "\n";
}
}

trait TestTrait
{
public function method()
{
print_r('Trait method');
print "\n";
}
}

class TestChildClass extends TestParentClass
{
use TestTrait
{
TestParentClass::method as methodParent;
}
}

(new TestChildClass)->methodParent();

@php-pulls
Collaborator

Comment on behalf of laruence at php.net:

wrong fix, this fix need more works than it look like.

@php-pulls php-pulls closed this
@reeze

What intend output in your code?

http://3v4l.org/UpMCW after the fix, 5.4's behavior is consist

@laruence
Owner

yes, it's consistent, but do you think it's right? do you think it's acceptable ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 69 additions and 0 deletions.
  1. +63 −0 Zend/tests/traits/bug64235.phpt
  2. +6 −0 Zend/zend_compile.c
View
63 Zend/tests/traits/bug64235.phpt
@@ -0,0 +1,63 @@
+--TEST--
+Bug #64235 (Insteadof not work for class method in 5.4.11)
+--SKIPIF--
+<?php
+if (!version_compare(zend_version(), '2.5.0', '<')) die('skip ZendEngine 2.4 only'); ?>
+?>
+--FILE--
+<?php
+
+class TestParentClass
+{
+ public function method()
+ {
+ print_r('Parent method');
+ print "\n";
+ }
+
+ public function bar()
+ {
+ print "bar\n";
+ }
+}
+
+trait TestTrait
+{
+ public function method()
+ {
+ print_r('Trait method');
+ print "\n";
+ }
+
+ public function bar()
+ {
+ print "bar trait";
+ }
+}
+
+class TestChildClass extends TestParentClass
+{
+ use TestTrait
+ {
+ TestTrait::method as methodAlias;
+
+ /* They should be traits but not classes */
+ TestTrait::bar insteadof TestParentClass;
+ TestParentClass::bar as newbar;
+ TestParentClass::method insteadof TestTrait;
+ }
+}
+
+(new TestChildClass)->method();
+(new TestChildClass)->methodAlias();
+echo "==DONE=="
+?>
+--EXPECTF--
+Deprecated: Using class as trait is deprecated, class: TestParentClass is not a trait in %sbug64235.php on line %d
+
+Deprecated: Using class as trait is deprecated, class: TestParentClass is not a trait in %sbug64235.php on line %d
+
+Deprecated: Using class as trait is deprecated, class: TestParentClass is not a trait in %sbug64235.php on line %d
+Parent method
+Trait method
+==DONE==
View
6 Zend/zend_compile.c
@@ -3859,6 +3859,12 @@ static void zend_check_trait_usage(zend_class_entry *ce, zend_class_entry *trait
{
zend_uint i;
+ /* Trait checking for 5.4 only which keep for BC, only trait is allowed 5.5+ */
+ if (!(trait->ce_flags & ZEND_ACC_TRAIT)) {
+ zend_error(E_DEPRECATED, "Using class as trait is deprecated, class: %s is not a trait", trait->name);
+ return;
+ }
+
for (i = 0; i < ce->num_traits; i++) {
if (ce->traits[i] == trait) {
return;
Something went wrong with that request. Please try again.