• php使用trait遇到的一个问题


    php是单继承语言,也是就是一个类只能继承一个单独的原始类
    自PHP5.4.0起,PHP实现了一种代码复用的方法,称为Trait
    trait 是在一些类(Class)的应该具备的特定的属性或方法,而同父级的另外一些类应该避免包含这些属性和方法情况下使用的.

    类成员优先级为:当前类>Trait>父类

     1 trait funcA {
     2             // 二、
     3             // trait关键字的使用和类定义一样
     4             // 也是可以定义方法和属性的
     5             public function a () {
     6                 echo "hello ";
     7             }
     8             public function b () {
     9                 echo "word ";
    10             }
    11             public function mm () {
    12                 echo "mmp";
    13             }
    14             public function c () {
    15                 $this->a();
    16                 $this->b();
    17             }
    18         }
    19         class A {
    20             // 在类A中使用use关键字,可以实现复用func里面的方法和属性(变相的继承了func中的属性和方法)
    21             use funcA;
    22         }
    23         $a = new A();
    24         $a->c();

    上面这段代码,最终输出结果为hello hello word

    经过断点调试,发现在实例化对象的时候,上述代码中的 a方法被执行了(难道是巧合?换个姿势在测试一次)

     1 trait funcA {
     2             // 二、
     3             // trait关键字的使用和类定义一样
     4             // 也是可以定义方法和属性的
     5             public function a () {
     6                 echo "hello";
     7             }
     8             public function b () {
     9                 echo "word";
    10             }
    11             public function c () {
    12                 $this->a();
    13                 $this->b();
    14             }
    15         }
    16         class B {
    17             // 在类A中使用use关键字,可以实现复用func里面的方法和属性(变相的继承了func中的属性和方法)
    18             use funcA;
    19         }
    20         $a = new B();
    21         // $a->c();

    按道理来说应该什么也不输出的,不巧的是,在实例化的时候trait中的b方法还是被执行了(那我们再换个姿势来测试一下?)

    trait funcA {
                // 二、
                // trait关键字的使用和类定义一样
                // 也是可以定义方法和属性的
                public function test1 () {
                    echo "hello";
                }
                public function test2 () {
                    echo "word";
                }
                public function c () {
                    $this->test1();
                    $this->test2();
                }
            }
            class A {
                // 在类A中使用use关键字,可以实现复用func里面的方法和属性(变相的继承了func中的属性和方法)
                use funcA;
            }
            $a = new A();

    按照我们之前的推断,这次应该不会再输出什么了吧!!对的没错,这次是按照我们的预期来得,所以上述原因是为什么呢?我也不晓得?待大神来答疑解惑。。。。。

  • 相关阅读:
    三层架构
    【Leetcode】Linked List Cycle II
    [Angular] @ContentChild with Directive ref
    [PostgreSQL] Use Foreign Keys to Ensure Data Integrity in Postgres
    [PostgreSQL] Ensure Uniqueness in Postgres
    [RxJS] Hot Observable, by .share()
    [RxJS] Implement pause and resume feature correctly through RxJS
    [RxJS] Replace zip with combineLatest when combining sources of data
    [RxJS] Use takeUntil instead of manually unsubscribing from Observables
    [RxJS] Convert RxJS Subjects to Observables
  • 原文地址:https://www.cnblogs.com/songdongdong/p/8777522.html
Copyright © 2020-2023  润新知