PHP在类中创建的对象的继承?

时间:2012-10-20 12:44:24

标签: php

我的课程Second通过First启动。我正在尝试直接访问$variable ,我应该可以使用$this->variable,但它似乎无效。

class First {
    public $variable;

    public function getSecond() {
        $this->variable = '123';
        $Second = new Second();
        $Second->blah();
    }
}

class Second extends First {
    public function blah() {
        // Trying to access the variable directly, it is not inherited though
        print_r($this->variable . 'test');
    }
}

$First = new First();
$First->getSecond();

输出:test,应输出123test

注意:我将这些功能公开用于测试目的。我真的想直接访问变量,而不是通过__construct传递它。

1 个答案:

答案 0 :(得分:2)

实例$ Second永远不会到达类方法getSecond(),因此$this->variable永远不会设置为'123'