我想动态添加/设置对象的特性。我该怎么办?
例如我有$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
方法。