<?php

trait Singleton
{
    private static $instance;

    /**
     * @param mixed ...$args
     * @return static
     */
    static function getInstance(...$args)
    {
        if (!isset(static::$instance)) {
            static::$instance = new static(...$args);
        }
        return static::$instance;
    }
}

class Test
{
    use Singleton;

    public function __construct($a, $b, $c)
    {
        echo $a, $b, $c;
    }
}
Test::getInstance(1, 2, 3);
// 输出123
最后修改:2023 年 12 月 30 日
如果觉得我的文章对你有用,请随意赞赏