This article is about abstract classes, interfaces and traits in PHP. Now I share it with you. Friends in need can also read the content of this article for reference.
Abstract class
Manual reference: http://php.net/manual/zh/language.oop5.abstract.php
Definition: PHP 5 supports abstract classes and abstract methods. Classes defined as abstract cannot be instantiated. Any class must be declared abstract if at least one method in it is declared abstract. AndWhen inheriting an abstract class, the subclass must define all abstract methods in the parent class (constants can be defined in the abstract class);
Keywords: abstract
<?php abstract class AbstractClass { const NAME='张三'; // 强制要求子类定义这些方法 abstract protected function getValue(); abstract protected function prefixValue($prefix); // 普通方法(非抽象方法) public function printOut() { print $this->getValue() . "\n"; } } class ConcreteClass1 extends AbstractClass { protected function getValue() { return "ConcreteClass1".self::NAME; } public function prefixValue($prefix) { return "{$prefix}ConcreteClass1"; } } $class1 = new ConcreteClass1; $class1->printOut(); echo $class1->prefixValue('FOO_') ."\n";
Interface
Keywords: interface
interface test1{ function say(); } interface test2{ function see(); } //接口继承接口 (继承接口时使用extends关键字) interface test3 extends test1,test2 { function sleep(); } //类实现接口(实现接口时使用 implements关键字) class test implements test1,test2{ public function say(){} public function see(){} public function sleep() { echo '休息'; } } //接口中只能有抽象方法(不能定义常量,不能有构造方法,不能有普通方法),且接口类中所有未实现的方法需要在子类中全部实现
Trait Implementing multiple inheritance
Reference address https://www.cnblogs.com/smallrookie/p/6516010.html
Definition :Starting from PHP 5.4.0, PHP has implemented a new code reuse method.
<?php trait A { public function smallTalk() { echo 'a'; } public function bigTalk() { echo 'A'; } } trait B { public function smallTalk() { echo 'b'; } public function bigTalk() { echo 'B'; } } class Aliased_Talker { use A, B { B::smallTalk insteadof A; //使用B类的smallTalk方法(替换A方法) A::bigTalk insteadof B; B::bigTalk as talk;//重命名 B类中的bigTalk方法重命名为talk方法 } } $obj = new Aliased_Talker; $obj->smallTalk(); //b $obj->bigTalk(); //A $obj->talk();//B //trait不能实例化,不能有常量
Related recommendations:
First introduction to php interface
Sharing the characteristics and functions of Trait in PHP
The above is the detailed content of abstract class,interface,trait. For more information, please follow other related articles on the PHP Chinese website!

博客,又译为网络日志、部落格或部落阁等,是一种通常由个人管理、不定期张贴新的文章的网站。那么怎么搭建博客?PHP博客系统有哪些?哪个博客系统好用?下面PHP中文网就来给大家总结分享十大开源php博客系统,一起来看看吧!

PHPtraitDTO:简化数据传输对象的开发引言:在现代的软件开发中,数据传输对象(DataTransferObject,简称DTO)起到了重要的作用。DTO是一种纯粹的数据容器,用于在层与层之间传递数据。然而,在开发过程中,开发人员需要编写大量的相似的代码来定义和操作DTO。为了简化这一过程,PHP中引入了trait特性,我们可以利用trait特

随着互联网的发展,博客成为越来越多人分享自己生活、知识和想法的平台。如果你也想创建一个自己的博客,那么本文将介绍如何使用PHP和SQLite来创建一个简单的博客。确定需求在开始创建博客之前,我们需要确定自己想要实现的功能。例如:创建博客文章编辑博客文章删除博客文章显示博客文章列表显示博客文章详情用户认证和权限控制安装PHP和SQLite我们需要安装PHP和S

深入了解PHPtraitDTO的设计模式与实践Introduction:在PHP开发中,设计模式是必不可少的一部分。其中,DTO(DataTransferObject)是一种常用的设计模式,用于封装数据传输的对象。而在实现DTO的过程中,使用trait(特征)可以有效地提高代码的复用性和灵活性。本文将深入探讨PHP中traitDTO的设计模式与实践

随着互联网的普及,博客在信息传播和交流方面扮演着越来越重要的角色。在此背景下,越来越多的人开始构建自己的博客网站。本文将介绍如何使用PythonDjango框架来构建自己的博客网站。一、PythonDjango框架简介PythonDjango是一个免费的开源Web框架,可用于快速开发Web应用程序。该框架为开发人员提供了强大的工具,可帮助他们构建功能丰

如何使用PHP创建一个简单的博客1.引言随着互联网的快速发展,博客已经成为了人们分享经验、记录生活和表达观点的一种重要方式。本文将介绍如何使用PHP来创建一个简单的博客,并附上具体的代码示例。2.准备工作在开始之前,你需要具备以下开发环境:一台安装了PHP解释器和Web服务器(如Apache)的计算机一个数据库管理系统,如MySQL一个文本编辑器或者IDE3

PHPtraitDTO:实现数据传输对象的简洁性与灵活性引言:在PHP开发过程中,经常会涉及到数据的传输与处理。而传输对象模式(DataTransferObject,简称DTO)是一种设计模式,它用于将数据在不同层之间传输。在传输过程中,DTO通过封装数据、提供公共访问方法来简化数据的操作。本文将介绍如何使用PHPtrait来实现DT

可以通过确定博客的主题和目标受众、选择合适的博客平台、注册域名和购买主机、设计博客的外观和布局、编写优质的内容、推广博客和分析和改进等步骤来创建博客。


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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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.

Zend Studio 13.0.1
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

SublimeText3 Chinese version
Chinese version, very easy to use