Class extend

Hi, I have a class B which extend class A. But the instantiation is done in different timing,for e.g:


$classA = new ClassA( )
$classA->setVar(1);

$classB = new ClassB( ) // extend ClassA
$classB->getVar(); // not able to access class A var because its a new instance.

In the above case, how do I 'link up" class B to class A?

I think that depends on how you have done setting the values. But normally you have to get the value from the same object with which you have set the value even it is the single class. Can you paste the code of both classes?

Also see the following manual page and see the examples given there.
http://www.php.net/manual/en/language.oop5.inheritance.php

The problem with your logic is that when a class extends another, it is merely reading off the original blueprint for that other class. It cannot read any instances of those classes.

So what I’d do in this instance is called Composition. It is where class B holds an instance of class A as a property. This is because you can do some interesting things, as I’ll show below, but for now here’s an example based on your post:

For instance:

class classA{
    protected $Var;
    public function setVar($Var){
        $this->Var = $Var;
    }
    public function getVar(){
        return $this->Var;
    }
}
class ClassB{
    protected $A;
    public function __Construct(ClassA $A){
        $this->A = $A;
    }
    public function getVar(){
        return $this->A->getVar();
    }
    public function setVar($Var){
        $this->A->setVar($Var);
    }
}

An example of this use could be an event being fired depending on the outcome of a flip of a coin, heads/tails, for example:

<?php
<?php
class Coin{
	protected $MaxRand;
	protected $HeadChance;
	protected $State;
	function __Construct($ProbHead, $ProbTail){
		$this->MaxRand = (int)$ProbHead + (int)$ProbTail;
		$this->HeadChance = (int)$ProbHead;
	}
	public function isHeads(){
		return $this->State;
	}
	public function isTails(){
		return !$this->isHeads();
	}
	public function flip(){
		$this->State = (rand(0, $this->MaxRand) <= $this->HeadChance);
	}
}
class FairCoin extends Coin{
	public function __Construct(){
		parent::__Construct(50, 50);
	}
}
class TrickCoin extends Coin{
	public function __Construct(){
		parent::__Construct(1, 0);
	}
}
class Gift{
	protected $Coin;
	public function setCoin(Coin $Coin){
		$this->Coin = $Coin;
	}
	public function chooseGift(){
		$this->Coin->flip();
		if($this->Coin->isHeads()){
			return 'Toy Robot';
		}else{
			return 'Necklace';
		}
	}
}

$Pattern =  '<li>Charlie can\\'t decide what to get his wife for christmas, so he flipped a coin. Fate decided, and he bought her a %s</li>';
$Coin = new FairCoin();
$Gift = new Gift();
$Gift->setCoin($Coin);
echo '<h3>Fair Coin</h3>';
echo '<ul>';
$HeadCount = 0;
for($i = 0; $i < 10; $i++){
	printf($Pattern, $Gift->chooseGift());
	if($Coin->isHeads()){
		$HeadCount++;
	}
}
echo '</ul>';
echo '<p>Chance of Robot: ' . $HeadCount * 10 . '%<p>';

$Coin = new TrickCoin();
$Gift->setCoin($Coin);
echo '<h3>Trick Coin</h3>';
echo '<ul>';
$HeadCount = 0;
for($i = 0; $i < 10; $i++){
	printf($Pattern, $Gift->chooseGift());
	if($Coin->isHeads()){
		$HeadCount++;
	}
}
echo '</ul>';
echo '<p>Chance of Robot: ' . $HeadCount * 10 . '%<p>';

$Coin = new Coin(7, 3); //so for every 10n throws, the most likely outcome is 7n heads.
$Gift->setCoin($Coin);
echo '<h3>Weighted Coin</h3>';
echo '<ul>';
$HeadCount = 0;
for($i = 0; $i < 10; $i++){
	printf($Pattern, $Gift->chooseGift());
	if($Coin->isHeads()){
		$HeadCount++;
	}
}
echo '</ul>';
echo '<p>Chance of Robot: ' . $HeadCount * 10 . '%<p>';

Here the second class used the first and had access to the former class’ its methods and properties, however it didn’t need to extend it.

The benefits of this type of composition are vast. A single object can be used across a system, and where it changes state in one part, it changes state in another.