如何不允许在PHP中定义子类方法
如何防止在foo类中创建下面的如何不允许在PHP中定义子类方法,php,class,methods,Php,Class,Methods,如何防止在foo类中创建下面的something方法 class fooBase{ public function something(){ } } class foo extends fooBase{ public function __construct(){ echo $this->something(); // <- should be the parent class method } public function something
something
方法
class fooBase{
public function something(){
}
}
class foo extends fooBase{
public function __construct(){
echo $this->something(); // <- should be the parent class method
}
public function something(){
// this method should not be allowed to be created
}
}
class-fooBase{
公共职能{
}
}
类foo扩展了fooBase{
公共函数构造(){
echo$this->something();//使用final
关键字(如Java等):
class-fooBase{
最终公共功能{
}
}
类foo扩展了fooBase{
公共函数构造(){
echo$this->something();//使用final关键字
在您的家长中:
final public function something()
您可以使用来防止基本方法被覆盖
class fooBase{
final public function something(){
}
}
\uu-construct
方法也可以是最终的(如果fooBase有一个)?是的,\uu-construct可以是最终的。如果你说它在父类中是最终的,那么在子类中就不能有一个。事实上,正如SamT所说,你可以使\uu-construct
成为最终的。