<?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
版权属于:
silverwq
作品采用:
《
署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)
》许可协议授权
评论 (1)