在php中接口抽象类、Final、Static几个我们用到的相当的简单特别是大型网站架构时都会有用到了,今天我们来看一篇关于php中抽象类、Final、Static的例子。
1. 接口
(1)对接口的使用是通过关键字implements
(2)接口不能定义成员变量(包括类静态变量),能定义常量
(3)子类必须实现接口定义的所有方法
(4)接口只能定义不能实现该方法
(5)接口没有构造函数
(6)接口中的方法和实现它的类默认都是public类型的
2. 抽象类
(1)对抽象类的使用是通过关键字extends
(2)不能被实例化,可以定义子类必须实现的方法
(3)子类必须定义父类中的所有抽象方法,这些方法的访问控制必须和父类中一样(或者更为宽松)
(4)如一个类中有一个抽象方法,则该类必须定义为抽象类
(5)抽象类可以有构造函数
(6)抽象类中的方法可以使用private,protected,public来修饰。
(7)一个类可以同时实现多个接口,但一个类只能继承于一个抽象类。
3. Final类/方法
(1)final类不能被继承
(2)final方法不能被重写
4. Static类/方法
(1)可以不实例化类而直接访问
(2)静态属性不可以由对象通过->操作符来访问,用::方式调用
例子
<?php # 接口 interface Human{ const TEST_CONST = "test const"; // 定义常量 // public $v; // error,不能定义变量 // static $count; // error,不能定义变量 public function speak(); public function walk(); public function run(); } # 抽象类 abstract class Father implements Human{ public function construct(){ echo "father init n"; } abstract public function walk(); // 抽象方法 public function speak(){ echo "father speak skill n"; } public function run(){ echo "father run skill n"; } } # 非抽象类 class Mother implements Human{ public function construct(){ echo "mother init n"; } # 这里必须实现walk方法 public function walk(){ echo "mother walk skill n"; } public function speak(){ echo "mother speak skill n"; } public function run(){ echo "mother run skill n"; } } class Son extends Father{ public function walk(){ echo "son walk skill. n"; } public function speak($name = ''){ echo "son: ". $name ." speak skill. n"; } # 访问控制必须和父类中一样(或者更为宽松) protected function sport(){ echo "son sport skill. n"; } final public function notTeach(){ echo 'son has not to teach skill'; } } class Daughter extends Mother{ public function run($name = ''){ echo "daughter run skill. n"; } } final class GrandChild extends Son{ # 访问控制必须和父类中一样(或者更为宽松) public function sport(){ echo "GrandChild sport skill. n"; } # Cannot override final method Son::notTeach() // public function notTeach(){} // error } # Class Orphan may not inherit from final class (GrandChild) // class Orphan extends GrandChild{} // error $son = new Son(); $son->speak("Suly"); $daughter = new Daughter(); $daughter->run('Lily'); $grandChild = new GrandChild(); $grandChild->sport();
抽象类和接口的区别
1、对接口的使用是通过关键字implements。对抽象类的使用是通过关键字extends。当然接口也可以通过关键字extends继承。
2、接口中不可以声明成员变量(包括类静态变量),但是可以声明类常量。抽象类中可以声明各种类型成员变量,实现数据的封装。(另JAVA接口中的成员变量都要声明为public static final类型)
3、接口没有构造函数,抽象类可以有构造函数。
4、接口中的方法默认都是public类型的,而抽象类中的方法可以使用private,protected,public来修饰。
5、一个类可以同时实现多个接口,但一个类只能继承于一个抽象类。
抽象类还是接口
如果要创建一个模型,这个模型将由一些紧密相关的对象采用,就可以使用抽象类。如果要创建将由一些不相关对象采用的功能,就使用接口。
如果必须从多个来源继承行为,就使用接口。
如果知道所有类都会共享一个公共的行为实现,就使用抽象类,并在其中实现该行为。
以上是php中接口、抽象类以及接口和抽象类区别详解的详细内容。更多信息请关注PHP中文网其他相关文章!

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

你应该关心DependencyInjection(DI),因为它能让你的代码更清晰、更易维护。1)DI通过解耦类,使其更模块化,2)提高了测试的便捷性和代码的灵活性,3)使用DI容器可以管理复杂的依赖关系,但要注意性能影响和循环依赖问题,4)最佳实践是依赖于抽象接口,实现松散耦合。

是的,优化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)优化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,并避免使用

theKeyStrategiestosiminificallyBoostphpapplicationPermenCeare:1)useOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)优化AtabaseInteractionswithPreparedStateTemtStatementStatementSandProperIndexing,3)配置

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增强codemodocultion,可验证性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

选择DependencyInjection(DI)用于大型应用,ServiceLocator适合小型项目或原型。1)DI通过构造函数注入依赖,提高代码的测试性和模块化。2)ServiceLocator通过中心注册获取服务,方便但可能导致代码耦合度增加。

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)启用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替换loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

phpemailvalidation invoLvesthreesteps:1)格式化进行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

SublimeText3 Linux新版
SublimeText3 Linux最新版

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)