search
HomeBackend DevelopmentPHP Tutorialabstract class,interface,trait
abstract class,interface,traitApr 04, 2018 pm 02:00 PM
traitblog

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=&#39;张三&#39;;
 // 强制要求子类定义这些方法
    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(&#39;FOO_&#39;) ."\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 &#39;休息&#39;;
  }

}
//接口中只能有抽象方法(不能定义常量,不能有构造方法,不能有普通方法),且接口类中所有未实现的方法需要在子类中全部实现



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 &#39;a&#39;;
    }
    public function bigTalk() {
        echo &#39;A&#39;;
    }
}
trait B {
    public function smallTalk() {
        echo &#39;b&#39;;
    }
    public function bigTalk() {
        echo &#39;B&#39;;
    }
}
 
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:

php object-oriented interface, inheritance, abstract classes, destruction, cloning and other advanced feature examples detailed explanation

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!

Statement
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 admin@php.cn
2022年十大开源php博客系统有哪些?【推荐】2022年十大开源php博客系统有哪些?【推荐】Jul 27, 2022 pm 05:38 PM

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

PHP trait DTO:简化数据传输对象的开发PHP trait DTO:简化数据传输对象的开发Oct 12, 2023 am 09:04 AM

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

创建一个简单的博客:使用PHP和SQLite创建一个简单的博客:使用PHP和SQLiteJun 21, 2023 pm 01:23 PM

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

深入了解PHP trait DTO的设计模式与实践深入了解PHP trait DTO的设计模式与实践Oct 12, 2023 am 08:48 AM

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

使用Python Django框架构建博客网站使用Python Django框架构建博客网站Jun 17, 2023 pm 03:37 PM

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

如何使用PHP创建一个简单的博客如何使用PHP创建一个简单的博客Sep 24, 2023 am 08:25 AM

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

PHP trait DTO:实现数据传输对象的简洁性与灵活性PHP trait DTO:实现数据传输对象的简洁性与灵活性Oct 12, 2023 am 10:21 AM

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

如何创建博客如何创建博客Oct 10, 2023 am 09:46 AM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SecLists

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

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use