Home > Backend Development > PHP Tutorial > abstract class,interface,trait

abstract class,interface,trait

不言
Release: 2023-03-23 08:10:02
Original
1462 people have browsed it

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";
Copy after login


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;;
  }

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



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不能实例化,不能有常量
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template