Blogger Information
Blog 43
fans 1
comment 0
visits 33470
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP 接口(interface)与trait
蔚蓝世纪
Original
907 people have browsed it

一、interface接口

使用接口(interface):可以指定某个类必须实现哪些方法,但不需要定义这些方法的具体内容。

  1. //继承单个接口
  2. interface monitor
  3. {
  4. const name = '张希希';
  5. }
  6. class myclass implements monitor//通过类来实现这个接口
  7. {
  8. public function write()
  9. {
  10. return myclass::name . '是我们的班长';
  11. }
  12. }
  13. echo (new myclass)->write();
  14. echo '<hr>';
  15. //继承多个接口
  16. interface monitor2
  17. {
  18. const name = '李原丁';
  19. }
  20. interface chargeman extends monitor2
  21. {
  22. const class1 = '五一班';
  23. }
  24. class myclass2 implements chargeman
  25. {
  26. public function write()
  27. {
  28. return monitor2::name . '是' .chargeman::class1 . '的班长。';
  29. }
  30. }
  31. echo (new myclass2)->write();
  32. echo '<hr>';
输出效果:

二、Trait

因为php是一种单继承语言,那么我们需要继承多个类的时候怎么办呢?于是php给了我们一个工具,就是“Trait”。

  1. Trait:基本功能是,为类似 PHP 的单继承语言而准备的一种代码复用机制。
  2. 我们仅仅需要在类中用use声明多个trait,这样当前类中的同名方法覆盖trait,而trait又覆盖基类中的同名方法,属性也是一样的。
1.trait优先级:从基类继承的成员会被 trait 插入的成员所覆盖。优先顺序是来自当前类的成员覆盖了 trait 的方法,而 trait 则覆盖了被继承的方法。

如:

  1. trait Hi{
  2. public function sayHi() {
  3. echo 'Hi, baby!';
  4. }
  5. }
  6. class Hy {
  7. use Hi;
  8. public function sayHi() {
  9. echo 'Hi , Girl!';
  10. }
  11. }
  12. $a = new Hy();
  13. $a->sayHi();

输出:

2.在同一个class中可以使用多个Trait

如:

  1. trait first_trait {
  2. function first_method()
  3. {
  4. echo $number = '1250,';
  5. }
  6. }
  7. trait second_trait {
  8. function second_method()
  9. {
  10. echo $number = '2';
  11. }
  12. }
  13. class first_class {
  14. use first_trait,second_trait;
  15. }
  16. $a = new first_class();
  17. $a->first_method();
  18. $a->second_method();

输出:

3.在多个class中可以同时使用一个Trait

如:

  1. trait Hi{
  2. public function sayHi1() {
  3. echo 'Hi, baby!';
  4. }
  5. }
  6. class Hy {
  7. use Hi;
  8. public function sayHi2() {
  9. echo 'Hi , Girl!';
  10. }
  11. }
  12. class Ha {
  13. use Hi;
  14. public function sayHi3() {
  15. echo 'Hi , Boy!';
  16. }
  17. }
  18. class Hb {
  19. use Hi;
  20. public function sayHi4() {
  21. echo 'Hi , Anna!';
  22. }
  23. }
  24. $a = new Hy();
  25. $b = new Ha();
  26. $c = new Hb();
  27. $a->sayHi1();
  28. $b->sayHi1();
  29. $c->sayHi1();
  30. ?>

输出:

4.trait之间也可以嵌套
  1. trait first_trait {
  2. function first_method()
  3. {
  4. echo $name1 = '张秀英 ';
  5. }
  6. }
  7. trait second_trait {
  8. use first_trait;
  9. function second_method()
  10. {
  11. echo $name2 = '& 赵云珂';
  12. }
  13. }
  14. class first_class {
  15. use second_trait;
  16. }
  17. $a = new first_class();
  18. $a->first_method();
  19. $a->second_method();

输出:

5.trait的抽象类用法

如:

  1. trait first_trait{
  2. function first_method()
  3. {
  4. echo $number = '50';
  5. }
  6. //这里可以加入修饰符,说明调用类必须实现它
  7. abstract public function second_method();
  8. }
  9. class first_class{
  10. use first_trait;
  11. function second_method(){
  12. echo $str = '这个数字是正确的。';
  13. }
  14. }
  15. $a = new first_class();
  16. $a->first_method();
  17. $a->second_method();

输出:

三、总结

  1. 每个类的定义都以关键字 class 开头,一个类可以包含有属于自己的常量,变量(称为“属性”)以及函数(称为“方法”)。
  2. 每个接口是通过 interface 关键字来定义的,就像定义一个标准的类一样,但其中定义所有的方法都是空的,这点类似于抽象类。
  3. 每个trait都是通过trait关键字来定义的,与class类似,但仅仅旨在用细粒度和一致的方式来组合功能。无法通过 trait 自身来实例化,这点类似于抽象类。
  4. classinterfacetrait都可以实现并列、嵌套。
  5. interfacetrait都需要通过class类来实例化。
  6. 多个class类可以同时调用一个trait,但是这几个类之间不需要继承。
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:接口之间允许多继承, 类不行, 而trait 正好可以弥补这个短板, 不得不说, php的设计者真是贴心
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!