PHPで親クラスのメソッドをどのように参照するか。
PHPでは、parent::キーワードを使用して親クラスのメソッドを参照することができます。たとえば、子クラスで親クラスのメソッドを呼び出す場合は、次のように書くことができます:
class ParentClass {
public function parentMethod() {
echo "This is parent method";
}
}
class ChildClass extends ParentClass {
public function childMethod() {
parent::parentMethod();
}
}
$child = new ChildClass();
$child->childMethod(); //输出:This is parent method
子クラスのメソッド内で、parent::parentMethod()を使用して親クラスのparentMethod()メソッドを呼び出します。