Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collisions with other trait methods

How can I deal with traits with methods of same name?

trait FooTrait {   public function fooMethod() {         return 'foo method';   }    public function getRow() {         return 'foo row';   } }  trait TooTrait {     public function tooMethod() {         return 'too method';     }      public function getRow() {         return 'too row';     } }  class Boo {     use FooTrait;     use TooTrait;      public function booMethod() {         return $this->fooMethod();     } } 

error,

Fatal error: Trait method getRow has not been applied, because there are collisions with other trait methods on Boo in...

What should I do about it?

And also, with two same method names, how can I get the method from trait FooTrait?

$a = new Boo; var_dump($a->getRow()); // Fatal error: Call to undefined method Boo::getRow() in...  

Edit:

class Boo {     use FooTrait, TooTrait {         FooTrait::getRow insteadof TooTrait;     }      public function booMethod() {         return $this->fooMethod();     } } 

what if I want to get the method getRow from TooTrait via Boo as well? Is it possible?

like image 328
Run Avatar asked Jul 31 '14 16:07

Run


People also ask

What is PHP treat?

Traits are a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.

Can traits be extended?

Traits are used to share interfaces and fields between classes. They are similar to Java 8's interfaces. Classes and objects can extend traits, but traits cannot be instantiated and therefore have no parameters.

What are traits in OOP?

Traits are used to declare methods that can be used in multiple classes. Traits can have methods and abstract methods that can be used in multiple classes, and the methods can have any access modifier (public, private, or protected).

What is difference between interface and traits in PHP?

The main difference between the Traits and Interfaces in PHP is that the Traits define the actual implementation of each method within each class, so many classes implement the same interface but having different behavior, while traits are just chunks of code injected in a class in PHP.


1 Answers

PHP Documentation about conflicts:

If two Traits insert a method with the same name, a fatal error is produced, if the conflict is not explicitly resolved.

To resolve naming conflicts between Traits used in the same class, the insteadof operator needs to be used to chose exactly one of the conflicting methods.

Since this only allows one to exclude methods, the as operator can be used to allow the inclusion of one of the conflicting methods under another name.

Example #5 Conflict Resolution

In this example, Talker uses the traits A and B. Since A and B have conflicting methods, it defines to use the variant of smallTalk from trait B, and the variant of bigTalk from trait A.

The Aliased_Talker makes use of the as operator to be able to use B's bigTalk implementation under an additional alias talk.

<?php trait A {     public function smallTalk() {         echo 'a';     }      public function bigTalk() {         echo 'A';     } }  trait B {     public function smallTalk() {         echo 'b';     }      public function bigTalk() {         echo 'B';     } }  class Talker {     use A, B {         B::smallTalk insteadof A;         A::bigTalk insteadof B;     } }  class Aliased_Talker {     use A, B {         B::smallTalk insteadof A;         A::bigTalk insteadof B;         B::bigTalk as talk;     } } 

So in your case it could be

class Boo {     use FooTrait, TooTrait {         FooTrait::getRow insteadof TooTrait;     }      public function booMethod() {         return $this->fooMethod();     } } 

(it works even if you do separate use, but i think it's more clear)

Or use the as to declare an alias.

like image 82
Marco Acierno Avatar answered Oct 05 '22 16:10

Marco Acierno