If they are separate classes you can do something like the following:
class A
{
private $name;
public function __construct()
{
$this->name = 'Some Name';
}
public function getName()
{
return $this->name;
}
}
class B
{
private $a;
public function __construct(A $a)
{
$this->a = $a;
}
function getNameOfA()
{
return $this->a->getName();
}
}
$a = new A();
$b = new B($a);
$b->getNameOfA();
What I have done in this example is first create a new instance of the A
class. And after that I have created a new instance of the B
class to which I pass the instance of A
into the constructor. Now B
can access all the public members of the A
class using $this->a
.
Also note that I don't instantiate the A
class inside the B
class because that would mean I tighly couple the two classes. This makes it hard to:
B
classA
class for another class