在PHP抽象類中,parent關(guān)鍵字通常用于引用父類中的屬性或方法。具體應(yīng)用包括:
class ParentClass {
public function __construct() {
echo "Parent constructor";
}
}
class ChildClass extends ParentClass {
public function __construct() {
parent::__construct();
echo "Child constructor";
}
}
$child = new ChildClass();
abstract class ParentClass {
public function method() {
echo "Parent method";
}
}
class ChildClass extends ParentClass {
public function method() {
parent::method();
echo "Child method";
}
}
$child = new ChildClass();
$child->method();
abstract class ParentClass {
public static function staticMethod() {
echo "Parent static method";
}
}
class ChildClass extends ParentClass {
public static function staticMethod() {
parent::staticMethod();
echo "Child static method";
}
}
ChildClass::staticMethod();
通過使用parent關(guān)鍵字,子類可以訪問和重用父類中的屬性和方法,從而實現(xiàn)代碼的復(fù)用和維護性。