开发者

php - extended classes carry objects

I tried to ask this question earlyer but I think I rushed the question and did not get accross what I was thinking...

Does the value and objects of $this get passed to an extened class?

<?php

class bar {

    public function foo( $t ) {

        $this->foo = $t;

    }

}

class foo extends bar {

    public function bar () {

        return $this->foo;  

    }

}

$b = new bar();
$b->foo('bar');
$f = new foo();
echo $f->bar开发者_运维知识库();

?>

if not, is there another decleration (instead of extends) that does without passing the object of the parent class to the child class?

regards,

Phil


Your example would yield an Undefined property: foo::$foo error. I think what you're trying to use is a static property:

class bar {
    protected static $foo;
    public function foo ($t) {
        static::$foo = $t;
    }
}

class foo extends bar {
    public function bar () {
        return static::$foo;  
    }
}

Then the following:

$b = new bar();
$b->foo('bar');
$f = new foo();
echo $f->bar();

... would echo bar, which looks like what is you're trying to achieve.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜