如果要添加抽象方法,那么您还需要使类抽象化.这样,该类无法实例化 – 只有非抽象子类才可以.
visibility(参见第二小节Method Visiblilty)在子类中不相同.根据您是否希望通过子类之外的代码调用方法,可以使类test中的(抽象)方法为public,或者使子类方法也受到保护.
请注意07002的第二段,其中解释了这一点:
When inheriting from an abstract class, all methods marked abstract in the parent’s class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private
abstract class test{
// Force Extending class to define this method
abstract protected function getValue();
abstract protected function prefixValue($prefix);
// Common method
public function printOut() {
print $this->getValue() . "\n";
}
}
class testabs extends test{
protected function getValue()
{
}
/**
* this method can be called from other methods with this class
* or sub-classes, but not called directly by code outside of this class
**/
protected function prefixValue($f)
{
}
}
$obj = new testabs();
// this method cannot be called here because its visibility is protected
$obj->prefixValues();// Fatal Error
?>