0

I want to dynamically add/set a Trait in object. How can I do it?

For example i have $object;

$object->setTrait($someTrait);

Thanks for help.
Edit

<?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;
            }
        });
    }
}

I want to test this class, and I think that I can make $object then dynamically insert Trait to the object and then test Trait boot method.

Kenny Horna
  • 13,485
  • 4
  • 44
  • 71
Davit Zeynalyan
  • 8,418
  • 5
  • 30
  • 55

0 Answers0