__CLASS__ will return the name o

2017-03-24  本文已影响0人  张霸天

__CLASS__will return the name of the class in which the trait is being used (!) not the class in which trait method is being called:



<?php
trait TestTrait {
    public function testMethod() {
        echo "Class: " . __CLASS__ . PHP_EOL;
        echo "Trait: " . __TRAIT__ . PHP_EOL;
    }
}

class BaseClass {
    use TestTrait;
}

class TestClass extends BaseClass {

}

$t = new TestClass();
$t->testMethod();

//Class: BaseClass
//Trait: TestTrait


上一篇 下一篇

猜你喜欢

热点阅读