Summary of php Abstract Class and Interface Knowledge Points

  • 2021-12-13 07:48:58
  • OfStack

This paper summarizes the knowledge points related to php abstract classes and interfaces with examples. Share it for your reference, as follows:

Abstract class (1 abstract class)

1. What is an abstract method?

Definition: If a method has no method body (a method that does not use {} and ends directly with a semicolon is a method without a method body), then this method is an abstract method.


class Person{
  abstract function say();  Having no method body   , using abstract  Keyword modification 
  abstract function say(){  This is not, there is a method body {} , 
};
}

Summary 1. Declare that a method does not use {}, but ends directly with a semicolon. 2. If it is an abstract method, you must use abstract (decorated with abstract keywords).

2. What are abstract classes?

1. If one method in a class is an abstract method, this class is an abstract class.

2. If an abstract class is declared, the class must be decorated with the abstract keyword.


abstract class Person{
  public $name;
  abstract function say();
  abstract function eat();
}

Attention

1. Any class decorated with abstract is an abstract class.

2. An abstract class is a special class, where is it special (there can be abstract methods in an abstract class).

3. Exactly like a normal class, except that there can be abstract methods in an abstract class.

Note 2

1. Abstract classes cannot instantiate objects (objects cannot be created).


$p=new Person(); // Report an error 

2. If you see an abstract class, you must write a subclass of this class and overwrite the abstract methods in the abstract class (plus the method body).


class student extend Person()
{
 function say(){  //  Overrides only the parent class's 1 A 
}
}

3. A subclass must fully implement (override) abstract methods before this subclass can create objects. If it implements some, then there are abstract methods, and it is still an abstract class.


class student extend Person()
{
 function say(){  //  Overrides only the parent class's 1 A 
}
}

Function of abstract method

1. Abstract method is a regulation, which stipulates that subclasses must have the implementation of this method, and the function is handed over to subclasses for implementation.

Only write out the structure, no implementation, implementation to specific subclasses (according to their own functions).


class student extend Person()
{
 function say(){
}
//  Overrides two abstract methods of the parent class 
function eat(){
}
}

Abstract class action

Is to require the structure of subclasses, so abstract classes are a specification. (Form only)

Interface (interface is a special abstract class, and interface is also a special class)

Similarities between interfaces and abstract classes

1. Both abstract classes and interfaces have abstract methods.

2. Abstract classes and interfaces cannot create instance objects.

3. Abstract classes and interfaces are used in the same way. Define 1 specification.

Differences

1. All methods in the interface must be abstract methods (non-abstract methods cannot be used), so abstract is not used in all methods of the interface, and semicolons are used directly.

2. Member attributes in the interface must be constants (no variables).

3. All permissions of the interface must be public public

4. Declare that the interface does not use class. Use interface,


interface Person{
 public $name; // Cannot declare variables   Report an error 
  const NAME='tom'//  Constants can be declared 
 function test();  // Because the interface is full of abstract methods   So   Omit  abstract  . 
 function test1();
Protect function test3()  Report an error   It can only be  public
}
$re=new Person; // Report an error   Can't   Create instantiation   Object   . 
echo Person:: NAME;  Output constant. 

Some details of interface application

1. You can use extends to have one interface inherit another (interface-to-interface relationships-only extension abstract methods, no override relationships).

2. You can use one class to implement all the methods in the interface, and you can also use one abstract class to implement some of the methods in the interface. (Classes and interfaces or abstract classes and interfaces have overriding relationships--overrides--implements abstract methods in interfaces)


interface Demo{
  const NAME='tom';
 public function test();
 public function test1();
}
interface test extends Demo{ // Interface to interface   Extension only 
 function test2();
}

3. As long as there is an override action in the subclass, do not use the keyword extends (inheritance extension), and use implements to implement it.


interface Demo{
  const NAME='tom';
 public function test();
 public function test1();
}
interface test extends Demo{ // Interface to interface   Extension only 
 function test2();
}
class Hello extends test{} //  Report an error  //  Class pair interface   Covered 
abstract class Hello implements test{
} // Do not report errors 

4.1 A class can implement an interface using implements while inheriting another class (multiple interfaces can be implemented) (1 must inherit first and implement the interface).


interface Demo{
  const NAME='tom';
  public function test();
public function test1();
}
interface test extends Demo{ // Interface to interface   Extension only 
 function test2();
}
Class Word{
Function test5(){
}
}
 class Hello extends Word implements test{
// You can use the interface 
function test(){
}
function test1(){
}
// Implementation interface 
function test2(){
}
//function test5(){
}
}

5. Implement multiple interfaces by separating them with commas.

A class in php has only one parent class.


abstract class Person{
  public $name;
  abstract function say();
  abstract function eat();
}

0

For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Syntax", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: