在PHP抽象类中,parent关键字通常用于引用父类中的属性或方法。具体应用包括:
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关键字,子类可以访问和重用父类中的属性和方法,从而实现代码的复用和维护性。