This article is a detailed analysis of the concepts and distinctions of abstract classes and interfaces in PHP, and the need for a friend to refer to the following
copy code code as follows:
Definition of
//abstract class:
abstract class ku{//define an abstract category
abstract function kx ();
......
}
function AA extends ku{
//Implementation of abstract class methods
function kx () {
echo ' SDSF ';
}
}
//Use method
$aa =new AA;
$aa->kx ();
//1. Defines methods that subclasses must fully implement in all of the methods in this abstraction
//2. An object cannot be created from an abstract class, and its meaning is extended
//3. Abstract classes usually have an abstract method with no curly braces in the method
//4. Abstract methods do not have to implement specific functions, subclasses to complete
//5. When a subclass implements a method of an abstract class, the visibility of its subclasses must be greater than or equal to the definition of the abstract method
//6. Abstract class methods can have parameters, or can be empty
//7. If the abstract method has parameters, the implementation of the subclass must also have the same number of arguments
//////////////////////////////Interface class definition:
Interface shop{
Public Function ($gid);
Public Function sell ($GID);
Abstract function view ($gid);
}
//If you want to use an interface, you must define a few of the methods in the interface class that are not available (except abstract).
//So if in a large project regardless of how others do the following method, but he must implement all the methods in this interface can!
//Example: a way to implement the above interface
class Baseshop implements shop{
Public Function Buy ($gid) {
Echo ' You purchased the ID for: '. $gid. ' Commodity ';
}
Public Function Sell ($gid) {
Echo ' You buy Sell ID: '. $gid. ' Commodity ';
}
Public Function View ($gid) {
Echo ' You browsed the ID for: '. $gid. ' Commodity ';
}
}
Multiple inheritance examples for
//interfaces:
<?php
interface staff_i1{//interface 1
function SetID ();
function GetID ();
}
interface staff_i2{//interface 2
function SetName ();
function GetName ();
}
class staff implements staff_i1,staff_i2{
Private $id;
private $name;
function SetID ($id) {
$this->id = $id;
}
function GetID () {
return $this->id;
}
function SetName ($name) {
$this->name = $name;
}
function GetName () {
return $this->name;
}
function Otherfunc () {//This is a method that does not exist in an interface
echo "Test";
}
}
?>
their different points:
1. Abstract classes can have non abstract methods, and interfaces can only have abstract methods!
2. A class can inherit multiple interfaces, and a class can inherit only one abstract class!
3. The use of the interface through the Implements keyword, the abstract class is through the inheritance extends keyword!