Last active
September 8, 2016 22:16
-
-
Save ramsey/bee9977cf72125ba728310d716bc997a to your computer and use it in GitHub Desktop.
Trait method collisions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace MyTraits; | |
| trait BarTrait | |
| { | |
| use BazTrait; | |
| public function bar() | |
| { | |
| return $this->baz() . ' : bar'; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace MyTraits; | |
| trait BazTrait | |
| { | |
| public function baz() | |
| { | |
| return 'baz'; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace MyTraits; | |
| trait FooTrait | |
| { | |
| use BazTrait; | |
| public function foo() | |
| { | |
| return $this->baz() . ' : foo'; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace MyNamespace; | |
| class MyClass | |
| { | |
| use \MyTraits\FooTrait, \MyTraits\BarTrait { | |
| \MyTraits\FooTrait::baz insteadof \MyTraits\BarTrait; | |
| } | |
| public function doSomething() | |
| { | |
| echo $this->foo(); | |
| echo "\n"; | |
| echo $this->bar(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require 'BazTrait.php'; | |
| require 'FooTrait.php'; | |
| require 'BarTrait.php'; | |
| require 'MyClass.php'; | |
| $myObj = new MyNamespace\MyClass(); | |
| $myObj->doSomething(); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PHP Fatal error: Trait method baz has not been applied, because there are collisions with other trait methods on MyNamespace\MyClass in MyClass.php on line 4