Skip to content

Instantly share code, notes, and snippets.

@ramsey
Last active September 8, 2016 22:16
Show Gist options
  • Select an option

  • Save ramsey/bee9977cf72125ba728310d716bc997a to your computer and use it in GitHub Desktop.

Select an option

Save ramsey/bee9977cf72125ba728310d716bc997a to your computer and use it in GitHub Desktop.
Trait method collisions
<?php
namespace MyTraits;
trait BarTrait
{
use BazTrait;
public function bar()
{
return $this->baz() . ' : bar';
}
}
<?php
namespace MyTraits;
trait BazTrait
{
public function baz()
{
return 'baz';
}
}
<?php
namespace MyTraits;
trait FooTrait
{
use BazTrait;
public function foo()
{
return $this->baz() . ' : foo';
}
}
<?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();
}
}
<?php
require 'BazTrait.php';
require 'FooTrait.php';
require 'BarTrait.php';
require 'MyClass.php';
$myObj = new MyNamespace\MyClass();
$myObj->doSomething();
@ramsey
Copy link
Copy Markdown
Author

ramsey commented Sep 8, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment