Home  >  Article  >  Backend Development  >  What is the use of php abstract class

What is the use of php abstract class

(*-*)浩
(*-*)浩Original
2019-10-10 09:18:022885browse

Abstract classes are often used to represent abstract concepts derived from analysis and design of problem areas. They are abstractions of a series of specific concepts that look different but are essentially the same.

What is the use of php abstract class

#Abstract class is incomplete, it can only be used as a base class. In the object-oriented approach, abstract classes are mainly used for type hiding and acting as global variables. (Recommended learning: PHP video tutorial)

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 calling method of the method must match, that is, the type and the number of required parameters must be consistent.

For example, if the subclass defines an optional parameter but it is not included in the declaration of the 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.

Abstract class example

<?php
abstract class AbstractClass
{
 // 强制要求子类定义这些方法
    abstract protected function getValue();
    abstract protected function prefixValue($prefix);

    // 普通方法(非抽象方法)
    public function printOut() {
        print $this->getValue() . "\n";
    }
}

class ConcreteClass1 extends AbstractClass
{
    protected function getValue() {
        return "ConcreteClass1";
    }

    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;) ."\n";

$class2 = new ConcreteClass2;
$class2->printOut();
echo $class2->prefixValue(&#39;FOO_&#39;) ."\n";
?>

The above routine will output:

ConcreteClass1
FOO_ConcreteClass1
ConcreteClass2
FOO_ConcreteClass2

The above is the detailed content of What is the use of php abstract class. For more information, please follow other related articles on the PHP Chinese website!

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