Home >Backend Development >PHP Tutorial >Detailed explanation of the difference between interfaces and abstract classes in PHP and how to define and inherit interface instance codes

Detailed explanation of the difference between interfaces and abstract classes in PHP and how to define and inherit interface instance codes

伊谢尔伦
伊谢尔伦Original
2017-07-03 10:19:091614browse

The difference between abstract classes and interfaces

Interfaces are special abstract classes and can also be regarded as the specification of a model. The general difference between an interface and an abstract class is as follows:

If a subclass implements an interface, it must implement all methods in the interface (whether needed or not); if it inherits an abstract class, it only needs to implement the required methods. Can.

If the method name defined in an interface changes, then all subclasses that implement this interface need to update the method name synchronously; and if the method name changes in an abstract class, the method name corresponding to its subclass will not be affected. The impact is just that it becomes a new method (implemented relative to the old method).

Abstract classes can only be inherited singly. When a subclass needs to implement functions that need to be inherited from multiple parent classes, interfaces must be used.

Code example:

<?php
//简单定义实现接口
interface UserInterface{  //定义user接口
    function getname();
}
interface TeacherInterface{    //定义teacher接口
    function getLengthofService();
}
class User implements UserInterface{ //实现user接口
    private $name="nostop";
    public function getName(){
        return $this->name;
    }
}
class Teacher implements TeacherInterface{ //实现teacher接口
    private $age=23;
    public function getLengthofService(){
        return $this->age;
    }
}
$user=new User();
echo $user->getName().&#39;<br />&#39;;//nostop
$teacher=new Teacher();
echo $teacher->getLengthofService().&#39;<br />&#39;;//23
//继承类实现接口
class GraduResultudent extends User implements TeacherInterface{ //继承User类 实现接口
    private $teacher;
    public function construct(){     //定义构造函数
        $this->teacher=new Teacher();  //实例化Teacher对象
    }
    public function getLengthOfService(){ //实现TeacherInterface接口的方法
        return $this->teacher->getLengthOfService();
    }
}
class Result{
    public static function getUserName(UserInterface $_user){ //注意:这里面的类型变成接口类型
        echo "Name is ".$_user->getName().&#39;<br />&#39;;
    }
    public static function getLengthOfService(TeacherInterface $_teacher){ //注意:这里面的类型变成接口类型
        echo "age is ".$_teacher->getLengthOfService();
    }
}
$object=new GraduResultudent();
Result::getUserName($object); //Name is nostop
Result::getLengthOfService($object); //age is 23
echo "<br />";
//接口实现用户的折扣
interface People{    //定义接口
    function getUserType();
    function getCount();
}
class VipUser implements People{ //实现接口
    //用户折卡系数
    private $discount=0.8;
    function getUserType(){
        return "VIP用户";
    }
    function getCount(){
        return    $this->discount;       
    }
}
$vip=new VipUser();    //实现化对象
echo $vip->getUserType().&#39;商品价格:&#39;.$vip->getCount()*100;  //VIP用户商品价格:80
class Goods{
    var $price=100;
    var $member;
    function run(People $member){ //注意:这里面的参数类型是接口类型
        $this->member=$member;
        $discount=$this->member->getCount();
        $usertype=$this->member->getUserType();
        echo $usertype."商品价格:".$this->price*$discount;
    }
}
$display=new Goods();
$display->run(new VipUser);//VIP用户商品价格:80
?>

The above is the detailed content of Detailed explanation of the difference between interfaces and abstract classes in PHP and how to define and inherit interface instance codes. For more information, please follow other related articles on the PHP Chinese website!

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