Answer the question
In order to leave comments, you need to log in
PHP how to solve multiple use trait conflict?
Good day everyone!
There was a conflict during the initialization in the class of two traits that use the same trait in themselves, I don’t catch crash and fatal errors yet (but I haven’t fully tested all these moments yet), but the IDE swears at such use, and I think quite reasonably, so I think how to optimize without a global census of the project architecture.
Detailed explanation of the conflict:
There is a main class (parent) that uses the error trait, there is a child class that inherits this error trait from the main one, and additionally uses its two traits (which also use the error trait internally). When initializing these two traits in a child class, a conflict occurs:
use OtherFirst, OtherSecond;
The IDE writes that the error trait methods from OtherSecond will not be used in the class, since they are already implemented in OtherFirst.
PS But in fact, I don't need to inherit from these two traits (their internal traits), since the error trait is already used in the parent class and is available for me to work inside the child classes. It is likely that I simply complicated my life with such a project architecture, but initially it seemed to me that I would get rid of unnecessary and repetitive code (which now and then jumps in almost every class, I wanted to minimize everything, divide it into different class files and traits, but now there are such conflicts). Can anyone share their idea and experience in solving such conflicts and problems.
PS2 I also already have ideas in mind, in general, do not use the error trait in other traits, but then you will have to write the same code for working with errors in each trait, and then initialize them through the main class, which is not desirable.
Answer the question
In order to leave comments, you need to log in
Alternatively, you can not use ErrorTrait in OtherFirstTrait and OtherSecondTrait, but make an abstract method with the same signature as in ErrorTrait. In ChildClass, in turn, use ErrorTrait, OtherFirstTrait, OtherSecondTrait.
Example:
trait ErrorTrait
{
public function someMethod($someArg) {
echo __TRAIT__ . ": " . $someArg . "\n";
}
}
trait OtherFirstTrait
{
abstract public function someMethod($someArg);
public function someMethod1($someArg) {
echo __TRAIT__ . ": " . $someArg . "\n";
$this->someMethod($someArg);
}
}
trait OtherSecondTrait
{
abstract public function someMethod($someArg);
public function someMethod2($someArg) {
echo __TRAIT__ . ": " . $someArg . "\n";
$this->someMethod($someArg);
}
}
abstract class MainClass
{
use ErrorTrait;
}
class ChildCLass extends MainClass
{
use OtherFirstTrait, OtherSecondTrait;
}
$o = new ChildCLass();
$o->someMethod("test ErrorTrait");
print("\n");
$o->someMethod1("test OtherFirstTrait");
print("\n");
$o->someMethod2("test OtherSecondTrait");
ErrorTrait: test ErrorTrait
OtherFirstTrait: test OtherFirstTrait
ErrorTrait: test OtherFirstTrait
OtherSecondTrait: test OtherSecondTrait
ErrorTrait: test OtherSecondTrait
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question