Access to method who defined in trait from object

时间:2015-10-29 15:59:32

标签: php oop

Example :

trait TaxTools {
function calculateтax ( $price ) {
return self::prop . $price ;
}
}

abstract class Alert{
    const prop = 'value__';

    abstract function limpopo();

}
class Alert2 extends Alert{
    use TaxTools;
   public  function limpopo(){
       // return self::$foo;
       return self::foo;
    }


}

$obj = new Alert2;

echo $obj->calculateтax(8); //value__8
echo '<br>';
echo $obj::calculateтax(8); //value__8

echo '<br>';
echo $obj::limpopo(); //Fatal error: Undefined class constant 'foo'

Question : Why we can access to method who defined in trait from object by :: syntax ?

1 个答案:

答案 0 :(得分:0)

Deprecated: Non-static method Alert2::calculateтax() should not be called statically in ... on line 28 

Deprecated: Non-static method Alert2::limpopo() should not be called statically in ... on line 31

这是不推荐使用的错误。如果您尚未打开显示已弃用的级别错误,则不会看到它。

开启它。

http://php.net/manual/ru/errorfunc.constants.php