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

伊谢尔伦
Release: 2023-03-12 13:02:01
Original
1487 people have browsed it

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:

name;
    }
}
class Teacher implements TeacherInterface{ //实现teacher接口
    private $age=23;
    public function getLengthofService(){
        return $this->age;
    }
}
$user=new User();
echo $user->getName().'
';//nostop $teacher=new Teacher(); echo $teacher->getLengthofService().'
';//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().'
'; } 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 "
"; //接口实现用户的折扣 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().'商品价格:'.$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 ?>
Copy after login

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!

Related labels:
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!