PHP 类是单继承,也就是不支持多继承,当一个类需要多个类的功能时,继承就无能为力了,为此 PHP 引入了类的接口技术。下面这篇文章主要跟大家介绍了关于PHP中关键字interface和implements的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。
PHP 接口
PHP 类是单继承,也就是不支持多继承,当一个类需要多个类的功能时,继承就无能为力了,为此 PHP 引入了类的接口技术。
如果一个抽象类里面的所有方法都是抽象方法,且没有声明变量,而且接口里面所有的成员都是 public 权限的,那么这种特殊的抽象类就叫 接口 。
接口使用关键字 interface 来定义,并使用关键字 implements 来实现接口中的方法,且必须完全实现。
类中接口的应用
1.关键字:interface
2.关键字:implements
1.接口的介绍与创建
接口:一种成员属性全部为抽象或常量的特殊抽象类。
规则:
1.类中全部为抽象方法。
2.抽象方法钱不用加abstract。
3.接口抽象方法属性为public。
4.成员属性必须为常量。
格式代码如下:
interface demo { const NAME = "常量对象属性"; function fun1(); function fun2(); //抽象方法。 }
2.接口的应用与规范
接口引用区别于类继承关键字 extends ,继承只能只是单一性,而接口可以使用关键字 implements 多个引用并用逗号分开
1.格式:普通类引用接口
class MyPc implements demo , demo2 , demo3 { ... }
2.格式:抽象类应用接口例子
abstract class MyPc implements demo , demo2 , demo3 { ... }
3.格式:继承父类引用接口并存
class MyPc extends Root implements demo , demo2 , demo3 { ... }
先继承后接口,单继承多接口。
4.格式:接口与接口的继承
interface demo3 extends demo { ... }
实例代码如下:
<?php interface demo { const NAME = "名称"; function fun1(); function fun2(); } interface demo2 { function fun3(); function fun4(); } interface demo3 { const TEST = "Test"; function fun5(); } class MyPc implements demo, demo2 { function fun1() { echo "++++++++++<br />"; } function fun2() { echo "----------<br />"; } function fun3() { echo "1111111111<br />"; } function fun4() { echo "2222222222<br />"; } } class MyPs extends MyPc implements demo3 { function fun5() { echo "继承类后引用接口"; } } $p = new MyPs; $p->fun1(); $p->fun2(); $p->fun3(); $p->fun4(); $p->fun5(); ?>
例,接口使用关键字 interface 来定义,并使用关键字 implements 来实现接口中的方法,且必须完全实现。
实例代码如下:
<?php //定义接口 interface User{ function getDiscount(); function getUserType(); } //VIP用户 接口实现 class VipUser implements User{ // VIP 用户折扣系数 private $discount = 0.8; function getDiscount() { return $this->discount; } function getUserType() { return "VIP用户"; } } class Goods{ var $price = 100; var $vc; //定义 User 接口类型参数,这时并不知道是什么用户 function run(User $vc){ $this->vc = $vc; $discount = $this->vc->getDiscount(); $usertype = $this->vc->getUserType(); echo $usertype."商品价格:".$this->price*$discount; } } $display = new Goods(); $display ->run(new VipUser); //可以是更多其他用户类型 ?>
运行该例子,输出:
VIP用户商品价格:80 元
该例子演示了一个 PHP 接口的简单应用。该例子中,User 接口实现用户的折扣,而在 VipUser 类里面实现了具体的折扣系数。最后商品类 Goods 根据 User 接口来实现不同的用户报价。
该例子仅限于演示 PHP 接口的用法,不涉及其科学与否。
实现多个接口
PHP也可以在继承一个类的时候同时实现多个接口:
class 子类 extends 父类 implemtns 接口1, 接口2, ... { ...... }
接口是特殊的抽象类,也可以看做是一个模型的规范。接口与抽象类大致区别如下:
1.一个子类如果 implements 一个接口,就必须实现接口中的所有方法(不管是否需要);如果是继承一个抽象类,只需要实现需要的方法即可。
2.如果一个接口中定义的方法名改变了,那么所有实现此接口的子类需要同步更新方法名;而抽象类中如果方法名改变了,其子类对应的方法名将不受影响,只是变成了一个新的方法而已(相对老的方法实现)。
3.抽象类只能单继承,当一个子类需要实现的功能需要继承自多个父类时,就必须使用接口。
<?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().'<br />';//nostop $teacher=new Teacher(); echo $teacher->getLengthofService().'<br />';//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().'<br />'; } 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().'商品价格:'.$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 A detailed introduction to interfaces and implements in PHP. For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!
