1

I try to access the parent's property from its extended child similar to the concept below,

class Base
{
    protected static $me;
    protected static $var_parent_1;
    protected static $var_parent_2;

    public function __construct ($var_parent_1 = null)
    {
        $this->var_parent_1 = $var_parent_1;
        $this->me = 'the base';
    }

    public function who() {
        echo $this->me;
    }

    public function parent_1() {
        echo $this->var_parent_1;
    }
}

class Child extends Base
{
    protected static $me;
    protected static $var_child_1;
    protected static $var_child_2;

    public function __construct ($var_child_1 = null)
    {
        parent::__construct();
        $this->var_child_1 = $var_child_1;
        $this->me = 'the child extends '.parent::$me;
    }

    // until PHP 5.3, will need to redeclare this
    public function who() {
        echo $this->me;
    }

    public function child_1() {
        echo $this->var_child_1;
    }
}

$objA = new Base($var_parent_1 = 'parent var 1');
$objA->parent_1(); // "parent var 1"
$objA->who(); // "the base"

$objB = new Child($var_child_1 = 'child var 1');
$objB->child_1(); // "child var 1"
$objB->who(); // should get "the child extends the base"

But I get "the child extends" instead of "the child extends the base" if I use $this->

It seems OK if I change all $this-> to self::

Why?

Is that the only proper way to access the parent's property which is to change all $this-> to self::?

EDIT:

I removed all static keywords,

class Base
{
    protected $me;
    protected $var_parent_1;
    protected $var_parent_2;

    public function __construct ($var_parent_1 = null)
    {
        $this->var_parent_1 = $var_parent_1;
        $this->me = 'the base';
    }

    public function who() {
        echo $this->me;
    }

    public function parent_1() {
        echo $this->var_parent_1;
    }
}

class Child extends Base
{
    protected $me;
    protected $var_child_1;
    protected $var_child_2;

    public function __construct ($var_child_1 = null)
    {
        parent::__construct();
        $this->var_child_1 = $var_child_1;
        $this->me = 'the child extends '.parent::$me;
    }

    // until PHP 5.3, will need to redeclare this
    public function who() {
        echo $this->me;
    }

    public function child_1() {
        echo $this->var_child_1;
    }
}

$objA = new Base($var_parent_1 = 'parent var 1');
//$objA->parent_1(); // "parent var 1"
//$objA->who(); // "the base"

$objB = new Child($var_child_1 = 'child var 1');
$objB->child_1(); // "child var 1"
$objB->who(); // "the child extends the base"

Then I get this error Fatal error: Access to undeclared static property: Base::$me in C:\wamp\www\test\2011\php\inheritence.php on line 109 which refers to this line,

$this->me = 'the child extends '.parent::$me;
Run
  • 54,938
  • 169
  • 450
  • 748
  • Are you sure you want those to be `static`? – jtbandes Aug 18 '11 at 21:51
  • not sure... I actually copied and modified an answer sample from this http://stackoverflow.com/questions/831555/php-child-class-accessing-parent-variable-problem – Run Aug 18 '11 at 22:01

2 Answers2

1

(simplified answer, I don't know how I could explain this without writing 20 pages, sorry).

At execution time, your base and child classes are merged in a single object. In that case, your instance variables are merged too. So, you can't call parent::$avar, you just can call $this->var (as all vars become elements of the current instance)

The behavior is quite different for methods : as the child class is supposed to be a specialization of the base class, the code written in the base class does not necessarily need to be completely rewritten : you can just call it and perform additional operations, without having te rewrite the original code in the child class.

Benjamin Dubois
  • 971
  • 7
  • 11
0

static properties are properties of the classes, not of the objects you create with them. a static property is shared by all objects of that class, and is accessed using self::

if you remove static it should work the way you want, and you would use $this-> as you are now.

Jonah
  • 15,806
  • 22
  • 87
  • 161
  • thanks. yes removed all the static keywords but then I get an error. please see my edit above. thanks. – Run Aug 18 '11 at 22:10
  • so you have now made $me a non-static, which it should be. but it makes no sense to ask which object is your object's parent? your object has a parent class, not a parent object. that is the reason for the new error you are seeing. remember that static refers to classes, and instance variables refer to objects. – Jonah Aug 18 '11 at 22:21