abstract class,interface,trait
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!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



Starting from scratch, I will teach you step by step how to install Flask and quickly build a personal blog. As a person who likes writing, it is very important to have a personal blog. As a lightweight Python Web framework, Flask can help us quickly build a simple and fully functional personal blog. In this article, I will start from scratch and teach you step by step how to install Flask and quickly build a personal blog. Step 1: Install Python and pip Before starting, we need to install Python and pi first

Blog, also translated as web log, blog or blog, is a website that is usually managed by individuals and posts new articles from time to time. So how to set up a blog? What are the PHP blog systems? Which blogging system is best to use? Below, PHP Chinese website will summarize and share the top ten open source PHP blog systems with you. Let’s take a look!

With the development of the Internet, blogs have become a platform for more and more people to share their lives, knowledge and ideas. If you also want to create a blog of your own, then this article will introduce how to use PHP and SQLite to create a simple blog. Determine the needs Before starting to create a blog, we need to determine the functions we want to achieve. For example: Create a blog post Edit a blog post Delete a blog post Display a list of blog posts Display blog post details User authentication and permission control Install PHP and SQLite We need to install PHP and S

PHPtraitDTO: Simplifying the development of data transfer objects Introduction: In modern software development, data transfer objects (DataTransferObject, referred to as DTO) play an important role. DTO is a pure data container used to transfer data between layers. However, during the development process, developers need to write a large amount of similar code to define and operate DTOs. In order to simplify this process, the trait feature was introduced in PHP. We can use the trait feature to

In-depth understanding of the design patterns and practices of PHPtraitDTO Introduction: In PHP development, design patterns are an essential part. Among them, DTO (DataTransferObject) is a commonly used design pattern used to encapsulate data transfer objects. In the process of implementing DTO, using traits can effectively improve the reusability and flexibility of the code. This article will delve into the design patterns and practices of traitDTO in PHP

With the popularity of the Internet, blogs play an increasingly important role in information dissemination and communication. In this context, more and more people are starting to build their own blog sites. This article will introduce how to use the PythonDjango framework to build your own blog website. 1. Introduction to the PythonDjango framework PythonDjango is a free and open source web framework that can be used to quickly develop web applications. The framework provides developers with powerful tools to help them build feature-rich

PHPtraitDTO: A key tool for optimizing the data transmission process. Specific code examples are required. Introduction: During the development process, data transmission is a very common requirement, especially when data is transferred between different levels. In the process of transmitting this data, we often need to process, verify or convert the data to meet different business needs. In order to improve the readability and maintainability of the code, we can use PHPtraitDTO (DataTransferObject) to optimize

How to create a simple blog using PHP 1. Introduction With the rapid development of the Internet, blogs have become an important way for people to share experiences, record life and express opinions. This article will introduce how to use PHP to create a simple blog, with specific code examples. 2. Preparation Before starting, you need to have the following development environment: a computer with a PHP interpreter and Web server (such as Apache) installed, a database management system, such as MySQL, a text editor or IDE3
