简体   繁体   中英

Access parent class properties from child class that have been set by parent constructor in PHP

OK, I'm having a bit of a problem. Here's the scenario: I need to be able to get the constructor of test2 to be able to access the class property test that is inside main_class that has been set by main_class' constructor. I'm not sure how to get it to work, and I need the system to work like exactly like this. Now, this WOULD work if I set the class variable in the code, like this var test = "hello"; in the class definition, but of course in this case, main_class::test is set by it's constructor and is not a "var", so it doesn't work.

Here is a highly simplified version of my code:

index.php:

<?php

class main_class
{
    private $test2;
    public function __construct()
    {
        $this->test2 = array();
        include("./test1.php");
        $var_name = "test";
        $this->$var_name = new test1();
    }

    protected function do_include()
    {
        include("./test2.php");
        $this->test2["test2"] = new test2();
    }
}

$test = new main_class();

?>

test1.php:

class test1 extends main_class
{
    public function __construct()
    {
        $this->do_include();
    }
}

?>

test2.php:

class test2 extends test1
{
    public function __construct()
    {
        print_r($this->test);
    }
}

?>

With this code, I get this error: Notice: Undefined property: test2::$test

Thanks in advance...

I suspect that part of the problem may be that you're not calling the parent constructor in your test2 class:

class test2 extends test1
{
    public function __construct()
    {
        parent::__construct();
        print_r($this->test);
    }
}

If that line is left out, then your test2 constructor overrides the test1 constructor completely, and $this->do_include() is never called.

Also, remember that when you call $this->test2["test2"] = new test2(); , you are creating a new instance of this class, which is not associated with the current one.

Just to clarify, here's the order of events:

$test = new main_class(); // calls the constructor of main_class:

public function __construct()
{
    $this->test2 = array();
    include("./test1.php");
    $var_name = "test";
    $this->$var_name = new test1();
}

Then:

$this->$var_name = new test1(); // calls the constructor of test1:

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

...which calls do_include() from main_class:

protected function do_include()
{
    include("./test2.php");
    $this->test2["test2"] = new test2();
}

Then:

$this->test2["test2"] = new test2(); // calls the constructor of test2:

public function __construct()
{
    print_r($this->test);
}

This creates a new object, and all you're doing in its constructor is printing a variable ($test) that does not exist yet...because you haven't done anything to create it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM