How to make a variable private to a trait?

后端 未结 4 661
广开言路
广开言路 2021-02-05 13:17

I\'d like to reuse a functionality several times in a single class. This functionality relies on a private variable:

tr         


        
相关标签:
4条回答
  • 2021-02-05 13:34

    i may be a little late for the party, but the behavior you are trying to create is not something that should be covered by a trait, but by simple object composition.

    <?php
    class Affffdress {
        private $street;
        private $number;
        public function __construct(string $street, ?string $number) {}
        public function street() : string {}
        public function number() : string {}
    }
    class User {
        private $homeAddress;
        private $workAddress;
        public function getHomeAddress() : Address {}
        public function setHomeAddress(Address $homeAddress) : self {}
        public function getWorkAddress() : Address {}
        public function setWorkAddress(Address $workAddress) : self {}
    }
    
    0 讨论(0)
  • 2021-02-05 13:34

    I confirmed that you can alias the same function multiple times, which was a surprise to me. Although ZendStudio appears to only 'code assist' on the last alias of the function.

    Being able to alias the same function multiple times could lend itself to some interesting behavior if the Trait function can determine what name it was called as. But it does not appear that we can determined the 'aliased' function within a trait function.

    Here's my test code:

    <?php
    trait TestTrait
    {
        public function test() { print __CLASS__ . ', ' . __TRAIT__ . ', ' . __METHOD__ . ', ' . __FUNCTION__  . "\n"; }
    }
    class TestClass
    {
        use TestTrait { test as test1; test as test2; }
    }
    $c = new TestClass();
    $c->test1();
    $c->test2();
    

    Output:

    TestClass, TestTrait, TestTrait::test, test
    TestClass, TestTrait, TestTrait::test, test
    

    Perhaps it would be nice to add a new __ALIAS__ constant for trait functions to determine what alias they were called as.

    In fact I have created PHP feature request for this:

    https://bugs.php.net/bug.php?id=63629

    0 讨论(0)
  • 2021-02-05 13:48

    Some years down the line, I followed up on a comment in bug 63629 and produced the following:

    <?php
    trait TestTrait
    {
        private $addresses = [];
        public function getAddress() {
            $calledAs = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0]['function'];
            return $this->addresses[substr($calledAs, 3)];
        }
    
        public function setAddress($address) {
            $calledAs = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0]['function'];
            $this->addresses[substr($calledAs, 3)] = $address;
        }
    }
    class TestClass
    {
        use TestTrait { getAddress as getHomeAddress; setAddress as setHomeAddress; }
        use TestTrait { getAddress as getWorkAddress; setAddress as setWorkAddress; }
    }
    $c = new TestClass();
    
    $c->setHomeAddress("High Street, Luton");
    echo $c->getHomeAddress();
    echo "\n";
    $c->setWorkAddress("Business Name, London");
    echo $c->getWorkAddress();
    echo "\n";
    

    which outputs

    High Street, Luton
    Business Name, London
    

    It can be done! (With thanks to Dave Farrell whose answer inspired this one.) The arguments to debug_backtrace are an attempt to minimize memory usage, I'm not certain how much of an effect on performance that is.

    0 讨论(0)
  • 2021-02-05 13:51

    Declaring a trait with use will not create an instance of that trait. Traits are basically just code that is copy and pasted into the using class. The as will only create an Alias for that method, e.g. it will add something like

    public function getHomeAddress()
    {
        return $this->getAddress();
    }
    

    to your User class. But it will still only be that one trait. There will not be two different $address properties, but just one.

    You could make the methods private and then delegate any public calls to it via __call by switch/casing on the method name and using an array for address, e.g.

    trait Address {
        private $address = array();
    
        private function getAddress($type) {
            return $this->address[$type];
        }
    
        private function setAddress($type, $address) {
            $this->address[$type] = $address;
        }
    
        public function __call($method, $args) {
            switch ($method) {
                case 'setHomeAddress':
                    return $this->setAddress('home', $args[0]);
                // more cases …
            }
        }
    }
    

    But that is just a can of worms.

    In other words, you cannot sanely do what you are trying to do with traits. Either use two different traits. Or use good old aggregation and add concrete proxy methods.

    0 讨论(0)
提交回复
热议问题