PHP Class/Object 函數(shù)參考手冊(cè)
trait_exists()函數(shù)檢查指定的 trait 是否存在。
trait_exists ( string $traitname [, bool $autoload ] )
檢查指定的 traitname 是否存在。
| 序號(hào) | 參數(shù)及說(shuō)明 |
|---|---|
| 1 | traitname(必需) 待檢查的 trait 的名稱。 |
| 2 | autoload(可選) 如果尚未加載,是否使用自動(dòng)加載(autoload)。 |
如果 trait 存在返回 TRUE,不存在則返回 FALSE。發(fā)生錯(cuò)誤的時(shí)候返回 NULL。
以下是此函數(shù)的用法-
<?php
trait World {
private static $instance;
protected $tmp;
public static function World()
{
self::$instance = new static();
self::$instance->tmp = get_called_class().' '.__TRAIT__;
return self::$instance;
}
}
if ( trait_exists( 'World' ) ) {
class Hello {
use World;
public function text( $str )
{
return $this->tmp.$str;
}
}
}
echo Hello::World()->text('!!!'); // Hello World!!!
?>測(cè)試看看 ?/?輸出結(jié)果:
Hello World!!!