PHP Abstract Class Interface Function Definition Method Example

  • 2021-11-29 06:13:39
  • OfStack

This paper describes the abstract classes, interface functions and definition methods in PHP. Share it for your reference, as follows:

Interfaces are introduced here, because abstract classes are not mentioned in several php reference books I have read recently.

I also think that after understanding the interface, abstract classes are also very easy to understand.

The example code is written casually under 1. Example code is very ok, the test will not report errors, too lazy to look at the code boots look at the text 1. Chestnuts don't lift well.

Code test environment: php 5.3. 29

Interface (interface):

Interface is born for abstraction, which is equivalent to a convention or specification, which is convenient for framework building and code division.

It specifies the name of abstract method and parameters and member constants, but it cannot contain any concrete methods and variables.

Personally, I think php is a weakly typed language, which emphasizes flexibility. Compared with java, the interface in php is too lax in type requirements. It is not easy to use, so it is not recommended to use in large quantities.

1) Interface is full of abstract methods. (Because it is used for subclass implementation. So if it's all public Or protected Of.) 2) There can be no concrete methods in the interface and only member constants. 3) Because php is different from java c + + and other strongly typed languages, php is a weak type and the dynamic type itself does not specify the return type, and the data type of parameters can be written or not except the four basic types, so there is no requirement for the method return value and the type of parameters. 4) A class can implement multiple interfaces. Usage: implements Keyword is implemented after multiple interfaces are separated by commas.

interface Car {
  const name = " Car ";
  public function run($speed=400,$time=300);
}
class Xiali implements Car {
  public function run($s=100 , $t=200){// The number of parameters must be the same 
    echo ' Ha ha   I can run ';
    return 'abc';
  }
  public function fly() {
    echo ' Ha ha I can fly ';
  }
}

Abstract class (abstract class):

Function and interface 1 are all to standardize the functions of subclasses, but they contain more specific contents than interfaces.

1) A class that contains at least one abstract method (abstract function) (in other words, any class that has one or more abstract methods must be an abstract class.)

2) Abstract class and interface 1 sample can not be instantiated. Are called abstract, also how to instantiate, instances are concrete. 2333.

3) Summary: Abstract class and ordinary class two differences: 1. Contains at least one abstract method 2. Can not instantiate. Everything else is 1.

4) Usage:


abstract class Father{
  abstract function func1();// At least 1 Abstract methods. 
  public function func2(){// Write the specific method casually. 
    echo 'func2';
    $this->func3();
  }
  private function func3(){
    echo '1 A private Method ';
  }
}

When implementing an inherited abstract method


/**
 *  Inheriting abstract class 
 *  You must implement all of its abstract methods 
 *  And ordinary 1 Sample, 1 Classes can only inherit 1 Abstract classes 
 */
class Son extends Father {
  public function func1() {
     echo ' Implement (override) abstract methods. The number of parameter names must be the same '
  }
}

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 Grammar", "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: