Home > Backend Development > PHP Tutorial > A brief analysis of the concepts and differences between abstract classes and interfaces in PHP_PHP Tutorial

A brief analysis of the concepts and differences between abstract classes and interfaces in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:03:11
Original
947 people have browsed it

Copy code The code is as follows:

//Definition of abstract class:
abstract class ku{ //Define an abstract class
abstract function kx();
......
}
function aa extends ku{
//Method to implement abstract class
function kx(){
echo 'sdsf';
}
}
//Usage method
$aa=new aa;
$aa->kx();
//1. Define some Methods, subclasses must fully implement all methods in this abstraction
//2. Objects cannot be created from abstract classes, its meaning is to be extended
//3. Abstract classes usually have abstract methods, and there are no Braces
//4. Abstract methods do not have to implement specific functions and are completed by subclasses
//5. When a subclass implements a method of an abstract class, the visibility of the subclass must be greater than or equal to abstract Definition of method
//6. The method of abstract class can have parameters or be empty
//7. If the abstract method has parameters, then the implementation of the subclass must also have the same number of parameters
////////////////////////////////Definition of interface class:
interface Shop{
public function buy($ gid);
public function sell($gid);
abstract function view($gid);
}
//If you want to use an interface, you must define all methods in the interface class with one less method No (except abstract).
//In this way, no matter how others do the following method in a large project, they must implement all the methods in this interface!
//Example: A method to implement the above interface
class BaseShop implements Shop{
public function buy($gid){
echo 'You purchased the product with ID:' . $gid . ';
}
public function sell( $gid){
echo 'You purchased and sold products with the ID:' . $gid . ';
}
public function view($gid){
echo 'You browsed the products with the ID :' . $gid . 'Product';
}
}
//Multiple inheritance example of interface:
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 the interface
echo “Test”;
}
}
?>

Their differences:
1. Abstract classes can have non-abstract methods, while interfaces can only have abstract methods!
2. A class can inherit multiple interfaces, but a class can only inherit one abstract class!
3. The interface is used through the implements keyword, and the abstract class is used by inheriting the extends keyword!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327877.htmlTechArticleCopy the code The code is as follows: //Definition of abstract class: abstract class ku{ //Define an abstract class abstract function kx(); ...... } function aa extends ku{ //Implement the method of abstract class fu...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template