Home >类库下载 >PHP类库 >Abstract classes and interface definitions in PHP

Abstract classes and interface definitions in PHP

高洛峰
高洛峰Original
2016-10-09 11:35:291305browse

Abstract classes and interface definitions in PHP

I will introduce interfaces first, because abstract classes are not mentioned in several PHP reference books I have read recently.

I also feel that abstract classes are very easy to understand after understanding interfaces.

The example code was written casually. The example code is very good, and no error will be reported after testing. Those who are too lazy to read the code will still see the text. Chestnuts are not lifted well.

Code testing environment: php5.3.29

Interface (interface):

Interface is born for abstraction, which is equivalent to an agreement or specification to facilitate framework construction and code division of labor.

It specifies the name, parameters and member constants of abstract methods, but cannot contain any specific methods or variables.

Personally, I think PHP is a weakly typed language and emphasizes flexibility. Compared with Java, the interface in PHP has too loose type requirements. Not very easy to use and not recommended for large amounts of use.

 1) All interfaces are abstract methods. (Because it is used to implement subclasses, it must be public or protected.)

 2) There cannot be specific methods in the interface, and there can only be member constants.

  3) Due to the difference between strongly typed languages ​​such as php and java c++, php is weakly typed and the dynamic type itself does not specify the return type. The data type of the parameter can be written or not except for the four basic types that cannot be specified, so for There are no requirements on the types of method return values ​​and parameters.

 4) A class can implement multiple interfaces. Usage: After the implements keyword, implement multiple interfaces separated by commas.

interface Car {
     const name = "车子";
        public function run($speed=400,$time=300);
}

class Xiali implements Car {
    public function run($s=100,$t=200){//参数个数必须相同
              echo '哈哈 我会跑';
              return 'abc';
       }
       
       public function fly() {
               echo '哈哈我会飞';
       }
}

Abstract class (abstract class): It has the same function as the interface, both to standardize the functions of the subclass, but it contains more specific content than the interface.

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

 2) Abstract classes cannot be instantiated like interfaces change. It’s called abstract, so why instantiate it? The examples are all concrete. 2333.

 3) Summary: The difference between abstract classes and ordinary classes: 1. Contains at least one abstract method 2. Cannot be instantiated. Everything else is the same.

 4) Usage:

abstract class Father{
        abstract function func1();//至少一个抽象方法。
            
        public function func2(){//具体方法随便写。
                echo 'func2';
                $this->func3();
        }   
        private function func3(){
                echo '一个private方法';
        }   
}

When implementing the inherited abstract method

/**
 * 继承抽象类
 * 必须实现其所有抽象方法
 * 和普通一样,一个类也只能继承一个抽象类
 */
class Son extends Father {
    public function func1() {
         echo '实现(重写)抽象方法。参数名个数必须相同'  
    }
}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more