Explain the abstract classes abstract methods and interface summary of PHP in detail

  • 2021-11-29 23:16:55
  • OfStack

The abstract classes and abstract methods in PHP are not used much by themselves, but they are often used by others in projects. Similarly, when looking at other people's code today, I find that abstract classes are used, so I summarize them as follows:

Abstract class:

1. If one method in one class is an abstract method, then this class is an abstract class;
2. Abstract classes must be decorated with abstract keywords;

Abstract method:

Definition: If a method has no method body, then this method is an abstract method
1. A method that has no method body and ends directly with a semicolon;
2. If it is an abstract method, it must be described by abstract abstract keyword

Note: 1. Any class decorated with abstract is an abstract class
2. Except that there can be abstract methods in abstract classes, it is completely like a normal class
3. Abstract classes cannot instantiate objects
4. If there is an abstract class, you must have a subclass of this class, and write methods in the subclass to override the abstract methods in the abstract class (plus the method body)
5. The subclass must completely override the abstract methods in the parent class before this subclass can be instantiated. If only the implementation part is realized, then this subclass is still an abstract class

The function of abstract class: It is to strictly require the structure of subclasses, so abstract class is actually a specification.

The function of abstract method is to stipulate that the subclass must have this method, and give the function to the subclass to handle, only write out the structure of the method, but not the implementation (no method body), and give the specific function implementation to the specific subclass to implement according to its own requirements


/**
*  Abstract class 
*
*/
abstract class Person
{
 public $name;
 public $age;

 // Abstract method 
 abstract function say();

 abstract function eat();

 function run()
 {
 echo "hello world";
 }

 function sleep()
 {
 echo "test";
 }
}


/**
*  Subclasses of abstract classes 
*
*/
class StudentCn extends Person
{
 function say()
 {
 echo " I speak Chinese <br>";
 }

 function eat()
 {
 echo " I eat with chopsticks ";
 }
}

Interface: Interface is a special abstract class

1. Both abstract classes and interfaces have abstract methods

2. Neither abstract classes nor interfaces can be instantiated

3. Abstract classes and interfaces have the same meaning (that is, function)

Compare:

a, the methods in the interface must all be abstract methods, so the abstract methods in the interface do not need to use the abstract keyword, and can be ended directly with a semicolon
b, member attribute in interface, must be constant (no variable)
c, all methods must be public
d, declaring that the interface does not apply to class, but uses interface

Tips:

1. You can use extends to let one interface inherit another interface, that is, the commonly used inheritance (extending new abstract methods) and no overriding relationship
2. One class can be used to implement all the methods in the interface, and one abstract class can also be used to implement some of the methods in the interface
3. Do not use the keyword extends. Using implements to implement implements is equivalent to extends
4. A class can inherit another class, and at the same time, use implements to implement one interface, and can also implement multiple interfaces (1 must inherit first, and then implement the interface)


/**
*  Interface 
*/
interface Demo
{
 const Name = " Sister ";
 const Age = 10;

 function test();

 function test2();
}
echo Demo::Name;// Access constant 

/**
*  Interface inherits interface and is used to extend interface 
*/
interface Test extends Demo
{
 function test4();
}

/**
*  General class 
*/
class world
{
 function test5()
 {
 }
}


/**
*  Inheritance + Interface 
*/
class Hello extends World implements Test
{

 function test()
 {
 }

 function test2()
 {
 }

 function test3()
 {
 }

 function test4()
 {
 }

 function test5()
 {
 }

}

Related articles: