1

I've been searching around the net for answers but couldn't find anything relevant, so I thought about asking it here.

How can I reach functions from a extended class through it's parent class?

Base (parent class)

require_once('FirstChild.class.php');

require_once('SecondChild.class.php');

class Base {

public $first;

public $second;


function __construct() {

$this->first = new FirstChild();

$this->second = new SecondChild();

}

}

First (child class)

class FirstChild extends Base {

public $firstVar;


function __construct() {

$this->firstVar = 'Hello';

}


public function getSecondVar() {

echo parent::$second->getVar();//doesnt work!!?

}

}

Second (child class)

class SecondChild extends Base {

public $secondVar;


function __construct() {

$this->secondVar = 'World';

}


public function getVar() {

return $this->secondVar;

}

}

How can the "getSecondVar" function be reached inside "FirstChild"?

Thanks!

1
  • self::method() or static::method() Commented Mar 10, 2013 at 13:28

2 Answers 2

1

Don't use the parent:: method. Instead use $this->second->getVar(); and make sure the parent constructor is called as well, e.g. with parent::__construct(); (alternatively, populate $this->second in your FirstChild constructor)

E.g.

class FirstChild extends Base {
    public function __construct() {
        // your code
        $this->second = new SecondChild();
        $this->firstVar = 'Hello';
    }
    public function getSecondVar() {
        echo $this->second->getVar();
    }
}

EDIT: Also the way you've set it up, $second would never be set as by adding the constructor method to FirstChild, you are overriding Base::__construct(). You'll either need to recall parent::__construct() and make sure it doesn't create a new instance of FirstChild() or you need to do the same code within FirstChild's constructor.

Anyway, it's usually not best practice to be calling child classes from a parent class.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help, but it seems to loop between the classes over and over again and I'm getting: Fatal error: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20 bytes) in Base.php on line 10
My bad, in this case you need to make sure that you don't call up the FirstChild in parent::__construct(). and instead call up only SecondChild within FirstChild::__construct(). See updated code
0

You read about how to reach a parents class function and it works with the parent keyword like so:

parent::__construct();

For the property $second you can just access with $this as it is public:

$this->second;

And that's already all. Take care you call the parent's constructor and keep in mind that public members are public (and you will never be able to reach private members).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.