I keep getting an php error indicating that either the method is not in the current class or there is a collision. See error:
Trait method callMe has not been applied, because there are collisions with other trait methods on Src\Classes\A in
C:\wamp\www\src\classes\a.php on line 72
I have tried but I could not find a solution. This is what I would like to achieve:
trait A // namespace Src\Traits;
{
function callMe()
{}
}
trait B // namespace Src\PowerTraits;
{
function callMe()
{}
}
class A // Namespace Src\Classes;
{
use \Src\Traits\A;
use \Src\PowerTraits\B {
A::callMe insteadof B::callMe;
}
}
When i try
A::callMe insteadof callMe;
i get the following error (understood, it is obviously in the wrong namespace):
Could not find trait Src\Classes\callMe
Also tried:
\Src\Traits\A::callMe insteadof \Src\Traits\B::callMe (error, syntax error);
\Src\Traits\A::callMe insteadof B::callMe (error: wrong namespace);
\Src\Traits\A::callMe as NewA (error, collision warning);
\Src\Traits\A::callMe as \Src\Traits\A::NewA (error, syntax error);
Left alone, gives the collision warning:
Trait method callMe has not been applied, because there are collisions with other trait methods
How can override a trait method when the traits and the calling class are all in different namespaces?
In regards to A::callMe insteadof B::callMe
::
(Paamayim Nekudotayim) when using insteadof
because PHP will look for a method by the same name. A::callMe insteadof B;
\\Src\\Traits\\A::callMe insteadof \\Src\\PowerTraits\\B;
Demo: https://3v4l.org/g1ltH
<?php
namespace Src\Traits
{
trait A
{
function callMe()
{
echo 'Trait A';
}
}
}
namespace Src\PowerTraits
{
trait B
{
function callMe()
{
echo 'PowerTrait B';
}
}
}
namespace Src\Classes
{
class A
{
use \Src\Traits\A;
use \Src\PowerTraits\B {
\Src\Traits\A::callMe insteadof \Src\PowerTraits\B;
}
}
}
namespace
{
(new \Src\Classes\A)->callMe();
}
Trait A
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.