tags:

views:

182

answers:

2

See the following example (PHP)

class Parent
{ 
  protected $_property;
  protected $_anotherP;

  public function __construct($var)
  {
    $this->_property = $var;
    $this->someMethod();  #Sets $_anotherP
  }

  protected function someMethod()
  ...
}

class Child extends Parent
{
  protected $parent;

  public function __construct($parent)
  {
    $this->parent = $parent;
  }

  private function myMethod()
  {
    return $this->parent->_anotherP;  #Note this line
  }
}

I am new to OOP and am a bit ignorant.

Here to access the parents property I am using an instance of that class, which seems wrong :S (no need of being i child then). Is there an easy way, so that i can sync the parent properties with the child properties and can directly access $this->anotherP without having to use $this->parent->anotherP ?

+2  A: 

As your Child class is extending your Parent class, every properties and methods that are either public or protected in the Parent class will be seen by the Child class as if they were defined in the Child class -- and the other way arround.

When the Child class extends the Parent class, it can be seen as "Child is a Parent" -- which means the Child has the properties of the Parent, unless it redefines those another way.

(BTW, note that "parent" is a reserved keyword, in PHP -- which means you can't name a class with that name)


Here's a quick example of a "parent" class :

class MyParent {
    protected $data;
    public function __construct() {
        $this->someMethodInTheParentClass();
    }
    protected function someMethodInTheParentClass() {
        $this->data = 123456;
    }
}

And it's "child" class :

class Child extends MyParent {
    public function __construct() {
        parent::__construct();
    }
    public function getData() {
        return $this->data; // will return the $data property 
                            // that's defined in the MyParent class
    }
}

That can be used this way :

$a = new Child();
var_dump($a->getData());

And you'll get as output :

int 123456

Which means the $data property, defined in the MyParent class, and initialized in a method of that same MyParent class, is accessible by the Child class as if it were its own.


To make things simple : as the Child "is a" MyParent, it doesn't need to keep a pointer to... itself ;-)

Pascal MARTIN
yeah. The call to the parent::__construct was what i needed :O
Shafee
Oh, yes, that's one of the tricky things, and I didn't think to mention it : if your child class defines a constructor, PHP will not call the constructor of the parent class -- which means you'll have to call it yourself *(see the note on http://fr.php.net/manual/en/language.oop5.decon.php )*
Pascal MARTIN
A: 

this should work..

class Child extends Parent
{
  private function myMethod()
  {
    return $this->_anotherP;
  }
}

because _anotherP its protected, so every derivated class can acces it, but its not in another object, its the same object.

However make getters an setters in the parent its a wise choice.

useless