如何在PHP中调用super? [英] How to call super in PHP?
本文介绍了如何在PHP中调用super?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个classB
扩展了classA
.在classB
中,我定义了方法fooBar()
,该方法也在classA
中定义.在classB
的fooBar()
中,我想一开始就调用classA
的fooBar()
.从Objective-C开始,这就是我惯用的方式.这在PHP中可行吗?如果可以,怎么办?
I have a classB
which extends classA
. In classB
I define a method fooBar()
which is also defined in classA
. In fooBar()
of classB
I want to call fooBar()
of classA
at the beginning. Just the way I'm used to, from Objective-C. Is that possible in PHP? And if so, how?
推荐答案
parent::fooBar();
直接从手册中:
...双冒号是允许访问...覆盖的类的属性或方法的令牌.
The ... double colon, is a token that allows access to ... overridden properties or methods of a class.
...
<?php
class MyClass
{
protected function myFunc() {
echo "MyClass::myFunc()\n";
}
}
class OtherClass extends MyClass
{
// Override parent's definition
public function myFunc()
{
// But still call the parent function
parent::myFunc();
echo "OtherClass::myFunc()\n";
}
}
$class = new OtherClass();
$class->myFunc();
?>
这篇关于如何在PHP中调用super?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文