Home >Backend Development >PHP Tutorial >Analysis of key points and precautions for using php abstract classes, php abstraction_PHP tutorial
This article analyzes the key points and precautions for using PHP abstract classes through examples. Share it with everyone for your reference. The specific analysis is as follows:
The key points and precautions for using PHP abstract classes are as follows:
1. Use abstract to modify a class, then this class is an abstract class; abstract classes must not be instantiated, that is, $abc = new abstract class name (); will report an error.
2. Use abstract to modify a method, then the method is an abstract method;
3. If there is an abstract method in a class, then the class must be defined as an abstract class; but conversely, an abstract class does not necessarily have an abstract method. In addition, abstract classes can also have ordinary methods.
4. Abstract methods cannot have method bodies. That is, abstract function abc();------cannot add curly brackets {..........} after it.
5. If a class inherits an abstract class, it must implement all the abstract methods in the abstract class (unless it also declares these abstract methods as abstract, which is equivalent to the abstract class inheriting the abstract class).
Simple instance of abstract class:
<?php abstract class Animal{ public $name; protected $price; abstract function cry(); } class Dog extends Animal{ function cry(){ echo "汪汪..."; } } $abc = new Animal(); ?>
I hope this article will be helpful to everyone’s PHP programming design.