php - 可以在对象中动态添加特征吗?
问题描述
我想在对象中动态添加/设置一个特征。我该怎么做?
例如我有$object
;
$object->setTrait($someTrait);
感谢帮助。
编辑
<?php
namespace GamApi\Models\Traits;
use GamApi\Scopes\OrganizationScope;
/**
*
* Trait BelongsToOrganizationTrait
* @package GamApi\Models\Traits
*/
trait BootOrganization
{
/**
* For saving resource auto-complete organization_id attribute
* Add global OrganizationScope
*
*/
public static function boot()
{
parent::boot();
// global organization_id scope
static::addGlobalScope(new OrganizationScope());
// set organization_id for saving
static::creating(function ($model){
if (defined('ORGANIZATION_ID')) {
$model->organization_id = ORGANIZATION_ID;
}
});
}
}
我想测试这个类,我认为我可以$object
动态地制作insert Trait to the object
然后测试Trait boot
方法。
解决方案
推荐阅读
- angular - Angular 和 FabricJS 类变量和事件
- c - 如何将另一个模块中的变量发送给函数?
- java - 在类型 [com.modelimpl.Modelmvcimpl] 上找不到属性 [fname]
- java - 尝试通过 JSON 查询从 openweathermap 获取数据时出现 FileNotFoundException
- excel - 直到循环执行缓慢 - 偏移和选择的替代方案?
- android - 查找设备的运动
- javascript - 如何使用 Selenium 进行硬刷新
- docker - 多台主机上的超级账本作曲家
- karate - 在空手道中与多个用户并行执行
- cakephp-3.0 - 使用反向路由时如何在cakephp3中设计链接