How to fixed trait method collision and rename method in php
one text
How to rename a method that conflict in PHP trait, rename and define insteadof
in a single line.
trait Hello
{
public function say()
{
return 'Hello';
}
}
trait World
{
public function say()
{
return 'World!';
}
}
This one works perfectly
class HelloWorld
{
use Hello, World {
Hello::say insteadof World;
Hello::say as sayHello;
World::say insteadof Hello;
World::say as sayWorld;
}
}
$obj=new HelloWorld();
echo $obj->sayHello(). ' '.$obj->sayWorld(); //Hello World
How to write in a single line instead of and rename look like this
//not working
class HelloWorld
{
use Hello, World {
Hello::say insteadof World as sayHello;
World::say insteadof Hello as sayWorld;
}
}
$obj=new HelloWorld();
echo $obj->sayHello(). ' '.$obj->sayWorld(); //nothing
Source