
trait_exists()函数检查指定的 trait 是否存在。
语法
trait_exists ( string $traitname [, bool $autoload ] )
定义和用法
检查指定的 traitname 是否存在。
参数
| 序号 | 参数及说明 | 
|---|---|
| 1 | traitname(必需) 待检查的 trait 的名称。 | 
| 2 | autoload(可选) 如果尚未加载,是否使用自动加载(autoload)。 | 
返回值
如果 trait 存在返回 TRUE,不存在则返回 FALSE。发生错误的时候返回 NULL。
在线示例
以下是此函数的用法-
<?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!!!
?>测试看看 ‹/›输出结果:
Hello World!!!
