开发者

Accessing parent methods in PHP

I've got a TopLevelClass that calls AnotherClass which has functions. From inside functions, how do you access some_other_methods() for TopLevelClass?

If it were JavaScript-esque my problem would look like:

$this->parent()->parent()->do_something()

and it would be equivalent to

$this_function->AnotherClass()->LevelClass()->some_other_m开发者_JAVA技巧ethods()


if you are using proper inheritance, you just need the parent keyword.

class foo {
    protected function fooMethod() {}
}
class bar extends foo {
    public function barMethod() {
        parent::fooMethod();
        // technically, you could do the same thing with $this->fooMethod()
        // but this way you also know how to do it with methods that might have
        // the same name as one another, such as parent::__construct()
    }
}


Out the top of my head:

parent::some_other_methods();


You could make AnotherClass extend TopLevelClass with:

class AnotherClass extends TopLevelClass {
  // class stuff in here
}

This would give AnotherClass access to all the methods in TopLevelClass as well as it's own (subject to Private scope status).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜