Home  >  Article  >  Backend Development  >  php--abstract class

php--abstract class

伊谢尔伦
伊谢尔伦Original
2016-11-23 14:10:341074browse

PHP 5 supports abstract classes and abstract methods. Classes defined as abstract cannot be instantiated. Any class must be declared abstract if at least one method in it is declared abstract. A method defined as abstract only declares its calling method (parameters) and cannot define its specific function implementation.

When inheriting an abstract class, the subclass must define all abstract methods in the parent class; in addition, the access control of these methods must be the same (or more relaxed) as in the parent class. For example, if an abstract method is declared as protected, then the method implemented in the subclass should be declared as protected or public, and cannot be defined as private. In addition, the method calling methods must match, that is, the type and number of required parameters must be consistent. For example, if a subclass defines an optional parameter that is not included in the declaration of an abstract method of the parent class, there is no conflict between the two declarations. This also applies to constructors since PHP 5.4. Constructor declarations before PHP 5.4 could be different.

Example #1 Abstract class example

abstract class AbstractClass
{
    //强制要求子类定义这些方法
    abstract protected function getValue();
    abstract protected function prefixValue($prefix);
    //普通方法(非抽象方法)
    public function printOut()
    {
        print $this->getValue().&#39;<br>&#39;;
    }
}
class ConcreteClass1 extends AbstractClass
{
    protected function getValue()
    {
        return "ConcteteClass1";
    }
    public function prefixValue($prefix){
        return "{$prefix}ConcreteClass1";
    }
}
class ConcreteClass2 extends AbstractClass
{
    public function getValue(){
        return "ConcreteClass2";
    }
    public function prefixValue($prefix){
        return "{$prefix}ConcreteClass2";
    }
}
$class1 = new ConcreteClass1;
$class1 -> printOut();
echo $class1->prefixValue(&#39;Foo_&#39;)."<br>";
$class2 = new ConcreteClass2;
$class2 -> printOut();
echo $class2->prefixValue(&#39;Bar_&#39;)."<br>";

Output result:

ConcteteClass1

Foo_ConcreteClass1

ConcreteClass2

Bar_ConcreteClass2

Example #2 Abstract class example

abstract class AbstractClass
{
    //我们的抽象方法仅需要定义需要的参数 
    abstract protected function prefixName($name);
}
class ConcreteClass extends AbstractClass
{
    //我们的子类可以定义父类签名中不存在的可选参数
    public function prefixName($name,$separator=&#39;.&#39;){
        if($name == &#39;Pacman&#39;){
            $prefix = "Mr";
        }elseif($name == &#39;Pacwoman&#39;){
            $prefix = &#39;Mrs&#39;;
        }else{
            $prefix = "";
        }
        return "{$prefix}{$separator} {$name}";
    }
}
$class = new ConcreteClass;
echo $class->prefixName(&#39;Pacman&#39;).&#39;<br>&#39;;
echo $class->prefixName(&#39;Pacwoman&#39;).&#39;<br>&#39;;

Output result:

Mr. Pacman

Mrs. Pacwoman


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn