There is no way to assign reference to a Class, nor is there a way to assign class constants at runtime. So your entire approach is pretty much impossible. What you can do is
const b = 'B'
and as of PHP 5.3.0 you could then do
$b = A::b;
echo $b::attr1;
but you cannot do A::b::attr1
. This will raise a T_PAAMAYIM_NEKUDOTAYIM
error. It’s a parser limitation. PHP cannot do this as of this writing.
Because the class B contains a group of data of A, i want to store in b (of A) a complex data, i want to do like this because i wanna keep the code clean
You can solve this easily by making B
a composite of A
, e.g. you either inject B
into A
when you create A
class A
{
private $b;
public function __construct(B $b)
{
$this->b = $b;
}
}
$a = new A(new B);
or create B
inside A
, e.g.
class A
{
private $b;
public function __construct()
{
$this->b = new B;
}
}
Because B
is just the data parts of A
, you tunnel all public access through A
instead of getting hold of B
and then using B’s methods. So any code using A
does not need to know there is some B
inside A
. This will allow you to change B
easily without needing to worry about code that consumes A
, e.g. to get attr1
you add a getter to A
:
public function getAttr1()
{
$b = $this->b;
return $b::attr1;
}
You can mitigate the clumsy need for assignment when using properties instead of constants in B
(constants are stupid in PHP anyway as you have to treat them as public API), e.g.
class B
{
private $attr1;
public function getAttr1()
{
return $this->attr1;
}
}
And then you can do in A
:
public function getAttr1()
{
return $this->b->getAttr1();
}
Even better would be not to expose the internals of B
through A
altogether though and only add public methods that do something with A. This will make your API much smaller and your code more OO.