Mock

prototype uses the 原型模式 , which will be automatically initialized when the framework is started.

Prototype mode

Gets scope for the prototype type of bean every time 克隆 initialization good bean .

Cloning an object is faster than re- new an object because it is a copy operation.

The collection of DB in swoft is the type of prototype used.

how to use

You can define a new method instead of the new keyword.

For example, Collection Prototype used by Db :

Entities are prototype , types of bean , and all entities can use the new method.

 <?php declare(strict_types=1);

namespace Swoft\Test;

use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Bean\Concern\PrototypeTrait;

/**
 * Class TestCollection
 *
 * @Bean(scope=Bean::PROTOTYPE)
 *
 * @since 2.0
 */
class TestCollection
{
    use PrototypeTrait;

    /**
     * @var array
     */
    private $items;

    /**
     * Create a new collection.
     *
     * @param array $items
     *
     * @return static
     */
    public static function new(array $items = []): self
    {
        $self        = self::__instance();
        $self->items = $items;

        return $self;
    }
} 

Need to introduce PrototypeTrait , implement the __instance() method in PrototypeTrait , which returns a clone's own object, you only need to update the property value to get a brand new object.

/docs/2.x/en/bean/prototype.html
progress-bar