Home >Backend Development >PHP Problem >What is the difference between interface and abstract class in php
The difference is: 1. The interface is defined through the interface keyword, and the abstract class is defined through the abstract keyword; 2. The interface has no data members, but the abstract class has data members, and the abstract class can implement Data encapsulation; 3. The interface does not have a constructor, but the abstract class can have a constructor.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
1. Abstract classes and interfaces Difference
When learning PHP object-oriented, people will be confused about abstract classes and interfaces. Why are they so easy to confuse when they have almost the same functions? Why not keep one and leave the other? But in fact, the difference between the two is still very big. If you can make good use of the two methods of PHP, object-oriented programming will be more reasonable, clear and efficient.
a. The interface is defined through the interface keyword, and the abstract class is defined through the abstract keyword.
b. The use of interfaces is achieved through the keyword implements, while the operation of abstract classes is implemented using the keyword extends of class inheritance. Pay special attention when using it.
c. Interfaces have no data members, but abstract classes have data members, and abstract classes can encapsulate data.
d. Interfaces do not have constructors, and abstract classes can have constructors.
e. The methods in the interface are all public types, while the methods in the abstract class can be modified with private, protected or public.
f. A class can implement multiple interfaces at the same time, but can only implement one abstract class.
Same points: Nothing can be written in the function bodies of abstract methods and interfaces, not even two curly brackets! ! ! For example: function getName(); This will do.
2. Interface
Using interface (interface), you can specify which methods a certain class must implement, but it is not required Define the specifics of these methods.
The interface is defined through the interface keyword, just like defining a standard class, but all methods defined in it are empty.
All methods defined in the interface must be public. This is a characteristic of the interface.
Implementations
To implement an interface, use the implements operator. The class must implement all methods defined in the interface, otherwise a fatal error will be reported. A class can implement multiple interfaces. Use commas to separate the names of multiple interfaces.
Note:
When implementing multiple interfaces, methods in the interfaces cannot have the same name.
Note:
Interfaces can also be inherited by using the extends operator.
Note:
To implement an interface, a class must use exactly the same method as the method defined in the interface. Otherwise a fatal error will result.
Constant
Constant can also be defined in the interface. Interface constants are used exactly the same as class constants, but cannot be overridden by subclasses or subinterfaces.
<?php // 声明一个'iTemplate'接口 interface iTemplate { public function setVariable($name, $var); public function getHtml($template); } // 实现接口 // 下面的写法是正确的 class Template implements iTemplate { private $vars = array(); public function setVariable($name, $var) { $this->vars[$name] = $var; } public function getHtml($template) { foreach($this->vars as $name => $value) { $template = str_replace('{' . $name . '}', $value, $template); } return $template; } } // 下面的写法是错误的,会报错,因为没有实现 getHtml(): // Fatal error: Class BadTemplate contains 1 abstract methods // and must therefore be declared abstract (iTemplate::getHtml) class BadTemplate implements iTemplate { private $vars = array(); public function setVariable($name, $var) { $this->vars[$name] = $var; } } ?> Example #2 可扩充的接口 <?php interface a { public function foo(); } interface b extends a { public function baz(Baz $baz); } // 正确写法 class c implements b { public function foo() { } public function baz(Baz $baz) { } } // 错误写法会导致一个致命错误 class d implements b { public function foo() { } public function baz(Foo $foo) { } } ?> Example #3 继承多个接口 <?php interface a { public function foo(); } interface b { public function bar(); } interface c extends a, b { public function baz(); } class d implements c { public function foo() { } public function bar() { } public function baz() { } } ?> Example #4 使用接口常量 <?php interface a { const b = 'Interface constant'; } // 输出接口常量 echo a::b; // 错误写法,因为常量不能被覆盖。接口常量的概念和类常量是一样的。 class b implements a { const b = 'Class constant'; } ?>
http://php.net/manual/zh/language.oop5.interfaces.php
3. Abstract class
PHP 5 Supports abstract classes and abstract methods. Classes defined as abstract cannot be instantiated. Any class must be declared abstract if at least one method in it is declared abstract. A method defined as abstract only declares its calling method (parameters) and cannot define its specific function implementation.
When inheriting an abstract class, the subclass must define all abstract methods in the parent class; in addition, the access control of these methods must be the same (or more relaxed) as in the parent class. For example, if an abstract method is declared as protected, then the method implemented in the subclass should be declared as protected or public, and cannot be defined as private. In addition, the method calling methods must match, that is, the type and number of required parameters must be consistent. For example, if a subclass defines an optional parameter that is not included in the declaration of an abstract method of the parent class, there is no conflict between the two declarations. This also applies to constructors since PHP 5.4. Constructor declarations before PHP 5.4 could be different.
<?php abstract class AbstractClass { // 强制要求子类定义这些方法 abstract protected function getValue(); abstract protected function prefixValue($prefix); // 普通方法(非抽象方法) public function printOut() { print $this->getValue() . "\n"; } } class ConcreteClass1 extends AbstractClass { protected function getValue() { return "ConcreteClass1"; } public function prefixValue($prefix) { return "{$prefix}ConcreteClass1"; } } class ConcreteClass2 extends AbstractClass { public function getValue() { return "ConcreteClass2"; } public function prefixValue($prefix) { return "{$prefix}ConcreteClass2"; } } $class1 = new ConcreteClass1; $class1->printOut(); echo $class1->prefixValue('FOO_') ."\n"; $class2 = new ConcreteClass2; $class2->printOut(); echo $class2->prefixValue('FOO_') ."\n"; ?> 以上例程会输出: ConcreteClass1 FOO_ConcreteClass1 ConcreteClass2 FOO_ConcreteClass2 Example #2 抽象类示例 <?php abstract class AbstractClass { // 我们的抽象方法仅需要定义需要的参数 abstract protected function prefixName($name); } class ConcreteClass extends AbstractClass { // 我们的子类可以定义父类签名中不存在的可选参数 public function prefixName($name, $separator = ".") { if ($name == "Pacman") { $prefix = "Mr"; } elseif ($name == "Pacwoman") { $prefix = "Mrs"; } else { $prefix = ""; } return "{$prefix}{$separator} {$name}"; } } $class = new ConcreteClass; echo $class->prefixName("Pacman"), "\n"; echo $class->prefixName("Pacwoman"), "\n"; ?> 以上例程会输出: Mr. Pacman Mrs. Pacwoman 老代码中如果没有自定义类或函数被命名为“abstract”,则应该能不加修改地正常运行。
http://php.net/manual/zh/language.oop5.abstract.php
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the difference between interface and abstract class in php. For more information, please follow other related articles on the PHP Chinese website!