I have three traits.
Trait Param, GroupId and SessionId. Traits GroupId and SessionId include Param.
Class GroupSession include traits GroupId and SessionId
<?php
trait Param
{
private $params = [];
public function setParams($params)
{
$this->params = $params;
}
public function getParam($param)
{
return isset($this->params[$param])
? $this->params[$param]
: null;
}
}
trait GroupId
{
use Param;
public function getGroupId()
{
return $this->getParam('group.id');
}
}
trait SessionId
{
use Param;
public function getSessionId()
{
return $this->getParam('session.id');
}
}
class GroupSession
{
use GroupId {
GroupId::setParams insteadOf SessionId;
GroupId::getParam insteadOf SessionId;
}
use SessionId;
}
$t = new GroupSession();
When I try to run this code I get an error:
GroupId and SessionId define the same property ($params) in the composition of GroupSession. However, the definition differs and is considered incompatible.
Tell me, please, how to resolve this problem?
Thank you!
GroupSessionimportedParaminstead ofGroupId/SessionIddoing so. The latter two traits just expect the importing class to provide a getParam/setParam method. SinceGroupSessionin your implementation has to take care of/modify the traits anyway, I don't think that's a disadvantage.