An in depth analysis of php's problems with abstract of abstract classes and abstract methods

  • 2020-12-07 03:59:40
  • OfStack

In the object-oriented (OOP) language, a class can have one or more subclasses, and each class can have at least one public method as an interface accessed by external code. Abstract methods were introduced to facilitate inheritance, so let's look at how abstract classes and abstract methods are defined and their characteristics in 1.

What is an abstract method? We only the method name inside the class definition of no method body method is abstract, the so-called no method body is in the method statement without braces as well as the content, but direct statement plus the semicolon after the method name, the other in statement abstract method to add a keyword "abstract" to modify.

1. Abstract key word: abstract

Abstract is a concept or a name that cannot be specified, but has a definite name. To declare an abstract class or method in PHP, we need to use the adstract keyword.

2. Definition of abstract methods and abstract classes

At least one of the methods in a class is abstract, which we call an abstract class. So if you define an abstract class you define an abstract method first.

abstract class class1{                                             
   abstract function fun1(); 

1. Have at least 1 abstract method in your class
2. Abstract methods are not allowed to have {}
3. Abstract methods must be preceded by abstract

3. Rules for abstract classes and methods

Some features of abstract classes:

1, cannot be instantiated, can only be inherited

2. All abstract methods must be overridden in an inherited derived class to be instantiated

The statements about abstract methods are as follows:

abstract function fun1();

What is an abstract class? As long as one of the methods in a class is abstract, the class must be defined as abstract. Abstract classes are also decorated with the keyword "abstract". Abstract classes cannot instantiate objects, so abstract methods are used as templates for subclass method overloading, and methods in inherited abstract classes are implemented.

Examples of abstract classes and their implementations are as follows:

abstract class User{  // Defining abstract classes 
    abstract protected function getUser(); // Defining abstract methods 
    public function print_content(){
        print $this->getUser();
class vipUser extends User{
    protected function getUser(){
        return " Abstract classes and abstract methods";
$user=new vipUser(); // Instantiate a subclass 
$user->print_content(); // Abstract classes and abstract methods 

Note: Abstract methods of a parent class cannot be overridden when an abstract class inherits another abstract class (intended as an extension of that abstract class).

In PHP5.1, static abstract methods are supported in abstract classes. In this example, you can see that static abstract methods can be declared. When you implement this method, it must be static.

abstract class User{
 protected static  $sal=0;
 static abstract function getSal();
 static abstract function setSal($sal); 
class VipUser extends User{
 static function getSal(){
  return self::$sal;
 static function setSal($sal){
echo "you sal is " . VipUser::getSal();

Related articles: