简体   繁体   中英

Get value of local variable from parent class?

I have two classes :

class A {
    public function load() {
        $var = new some_class();
        return $var;
    }
}

and

class B extends A {
    public function test() {
        $this->load();
        $var->some_method(); // ERROR : variable "$var" does not exists
    }
}

I want to access the local variable $var of the class "A" from class "B", how can i do this?

just fill $var with $this->load();

class B extends A {
    public function test() {
        $var = $this->load();
        $var->some_method(); // WORKS
    }
}

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