Skip to content

Commit 9a44a98

Browse files
committed
Fixed bug #64235 (Insteadof not work for class method in 5.4.11)
As we discussed with stefan, we think previous of allowing use with classes is a bug, should be forbided, anyway, the error message should be improved.
1 parent bae95bd commit 9a44a98

File tree

7 files changed

+79
-4
lines changed

7 files changed

+79
-4
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ PHP NEWS
33
?? ??? 2012, PHP 5.4.13
44

55
- Core:
6+
. Fixed bug #64235 (Insteadof not work for class method in 5.4.11).
7+
(Laruence)
68
. Implemented FR #64175 (Added HTTP codes as of RFC 6585). (Jonh Wendell)
79
. Fixed bug #64142 (dval to lval different behavior on ppc64). (Remi)
810
. Fixed bug #64070 (Inheritance with Traits failed with error). (Dmitry)

Zend/tests/traits/bug64235.phpt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
Bug #64235 (Insteadof not work for class method in 5.4.11)
3+
--FILE--
4+
<?php
5+
6+
class TestParentClass
7+
{
8+
public function method()
9+
{
10+
print_r('Parent method');
11+
print "\n";
12+
}
13+
}
14+
15+
trait TestTrait
16+
{
17+
public function method()
18+
{
19+
print_r('Trait method');
20+
print "\n";
21+
}
22+
}
23+
24+
class TestChildClass extends TestParentClass
25+
{
26+
use TestTrait
27+
{
28+
TestTrait::method as methodAlias;
29+
TestParentClass::method insteadof TestTrait;
30+
}
31+
}
32+
?>
33+
--EXPECTF--
34+
Fatal error: Class TestParentClass is not a trait, Only traits may be used in 'as' and 'insteadof' statements in %sbug64235.php on line %d

Zend/tests/traits/bug64235b.phpt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
Bug #64235 (Insteadof not work for class method in 5.4.11)
3+
--FILE--
4+
<?php
5+
6+
class TestParentClass
7+
{
8+
public function method()
9+
{
10+
print_r('Parent method');
11+
print "\n";
12+
}
13+
}
14+
15+
trait TestTrait
16+
{
17+
public function method()
18+
{
19+
print_r('Trait method');
20+
print "\n";
21+
}
22+
}
23+
24+
class TestChildClass extends TestParentClass
25+
{
26+
use TestTrait
27+
{
28+
TestTrait::method as methodAlias;
29+
TestParentClass::method as TestParent;
30+
}
31+
}
32+
33+
?>
34+
--EXPECTF--
35+
Fatal error: Class TestParentClass is not a trait, Only traits may be used in 'as' and 'insteadof' statements in %sbug64235b.php on line %d

Zend/tests/traits/language015.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ class C {
1414
}
1515
}
1616
--EXPECTF--
17-
Fatal error: Trait T2 is not used in %s on line %d
17+
Fatal error: Required Trait T2 wasn't added to C in %slanguage015.php on line %d

Zend/tests/traits/language016.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ class C {
1414
}
1515
}
1616
--EXPECTF--
17-
Fatal error: Trait T2 is not used in %s on line %d
17+
Fatal error: Required Trait T2 wasn't added to C in %slanguage016.php on line %d

Zend/tests/traits/language017.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ class C {
1414
}
1515
}
1616
--EXPECTF--
17-
Fatal error: Trait T2 is not used in %s on line %d
17+
Fatal error: Required Trait T2 wasn't added to C in %slanguage017.php on line %d

Zend/zend_compile.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3859,12 +3859,16 @@ static void zend_check_trait_usage(zend_class_entry *ce, zend_class_entry *trait
38593859
{
38603860
zend_uint i;
38613861

3862+
if ((trait->ce_flags & ZEND_ACC_TRAIT) != ZEND_ACC_TRAIT) {
3863+
zend_error(E_COMPILE_ERROR, "Class %s is not a trait, Only traits may be used in 'as' and 'insteadof' statements", trait->name);
3864+
}
3865+
38623866
for (i = 0; i < ce->num_traits; i++) {
38633867
if (ce->traits[i] == trait) {
38643868
return;
38653869
}
38663870
}
3867-
zend_error(E_COMPILE_ERROR, "Trait %s is not used", trait->name);
3871+
zend_error(E_COMPILE_ERROR, "Required Trait %s wasn't added to %s", trait->name, ce->name);
38683872
}
38693873
/* }}} */
38703874

0 commit comments

Comments
 (0)