Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Parent Class' property from child

Tags:

oop

php

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 ?

like image 781
shxfee Avatar asked Feb 17 '10 19:02

shxfee


People also ask

How do you access parent class from a child?

The super keyword is used to access data members of the parent class when the parent and child class have a member with the same name. The super keyword is used to explicitly call the no-arg and parameterized constructor of the parent class.

How do I access parent class properties?

To access parent class attributes in a child class: Use the super() method to call the constructor of the parent in the child. The __init__() method will set the instance variables. Access any of the parent class's attributes or methods on the self object.

Can a child class inherit from a parent class?

The class whose properties and methods are inherited is known as the Parent class. And the class that inherits the properties from the parent class is the Child class. The interesting thing is, along with the inherited properties and methods, a child class can have its own properties and methods.

Can a child class access parent constructor?

A parent class constructor is not inherited in child class and this is why super() is added automatically in child class constructor if there is no explicit call to super or this.


2 Answers

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 ;-)

like image 194
Pascal MARTIN Avatar answered Oct 11 '22 17:10

Pascal MARTIN


This may save you a few hours of searching around.

Remember: Your Child class only inherits the properties DEFINED in the Parent class... So if you instantiate an object using Parent class and then populate it with data, then this data will NOT be available in your child class...

It's super obvious of course, but I'm guessing others may run into the same issue.

A super simple solution is not to extend anything, simply pass the $object of your parent class into your child class through a constructor. This way you have access to all the properties and methods of the object generated by parent class

Example

class child {

    public parentObject;

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

}

If your $parentObject has a public property $name, then you can access it inside the child class with a function like:

public function print_name() {
    echo $this->parentObject->name;
}
like image 22
Robert Sinclair Avatar answered Oct 11 '22 19:10

Robert Sinclair