最近,我遇到了一个在类定义内使用use语句的类。
有人可以解释它的确切作用-因为我找不到有关它的任何信息。
我知道这可能是将其从给定文件的全局范围中移出的一种方法,但是它也许还允许给定类也从多个父类继承-因为use仅允许一个父类引用?
我看到的示例在Laravel原始安装的User模型中:
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
}
并且我已经看到了一些使用use类中包含的方法的该模型的示例,因此引起了我的怀疑,但我确实想进一步了解封闭的use语句的含义。
PHP文档说:
use关键字必须在文件的最外层范围内声明( 全局范围)或内部名称空间声明。 这是因为 导入是在编译时而不是在运行时完成的,因此不能 块作用域。 以下示例将显示非法使用 使用关键字:
接下来是示例:
namespace Languages;
class Greenlandic
{
use Languages\Danish;
...
}
这将表明它是use关键字的不正确使用-有任何线索吗?