How to make a variable private to a trait?
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 Adddress {
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 {}
}
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.
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