2

Can trait precedence rules be used on properties? My initial research reveals nothing and tests have yielded no....

trait ReuseThis{
   public  $Dependency_Property;
   public function mutateProperty(){
      //...long method
   }
}
trait privatePropInstead{
     private $Dependency_Property;
}

class precedence_test{
     //would like to reuse long methods of ReuseThis, but with private properties
        use ReuseThis,privatePropertyInstead{
            //all of these fail 
           //privatePropInstead->Dependency_Property insteadof ReuseThis;
           //privatePropInstead::$Dependency_Property insteadof ReuseThis;
           //privatePropInstead::Dependency_Property insteadof ReuseThis;


        }
}
user2782001
  • 3,380
  • 3
  • 22
  • 41

1 Answers1

0

In the case of property - it does not matter where it was declared, what matters is the value that the property carries.

value problem can and should be solved by assigning value to the property and it should be done in the public function __construct(). It is the entire purpose of this function.

PS

The value assignment would not work correctly in static properties, but I strongly discourage everyone from using static properties at all. Just use another class as a singleton... well, it is a talk for a different question.

Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
  • Could you please provide example how 'value problem' can be solved by assigning value to conflicting property in constructor? I performed experiment: created 2 traits and class. In traits I defined the same non-static property, but with different definition. Then used both traits in class and in constructor assigned value to conflicting property from traits. I got PHP Fatal error attempting to create instance of this class. Maybe doing smth wrong, but for now, I think it's not possible to use traits with same properties... – Jakhongir Apr 16 '20 at 05:37
  • I use http://phpfiddle.org/ for testing side tasks. If you please can make your code work there and pass me it somehow, I'd be happy to look closer. Then we can discuss it. – Yevgeniy Afanasyev Apr 17 '20 at 01:58
  • Rereading your post and writing code in phpfiddle.org I realized that I probably got the meaning of your post wrong, this is what I firstly thought you post means: "Conflict between same properties from different traits can be resolved by reassigning value to the property in constructor". Now I think what you meant is: "properties' values should never be set in traits, but in class constructor", could you confirm? – Jakhongir Apr 17 '20 at 06:52
  • Yes, the second one is what I tried to say. Please correct my wording if it is not clear. English is my second language. – Yevgeniy Afanasyev Apr 19 '20 at 10:21
  • So do mine, maybe that's why I got it wrong at first:) But IMHO: I think it would be better if in the post, there would be an info about how to handle conflicting properties from different traits. Or confirmed that it is not possible now and traits with same properties and same value just should not be used together - and maybe this is because of the reason you gave in the post 'properties should not be initialized in traits but in constructor'. – Jakhongir Apr 21 '20 at 17:54
  • I can give you my reasons. If you assign variables in traits it will be confusing. And even if you figure out the rules, they may change in the future with new version of PHP. – Yevgeniy Afanasyev Apr 22 '20 at 09:17