<?php

// 使用trait 关键词来创建一个 Singleton 作为一个每个类服用的单例类
trait  Singleton{

    // 需要一个静态变量来存储自己
    private static $instance;

    public static function getInstance(...$param)
    {
        
        // 判断是否为自己本身创建了单例
        if (! isset(self::$instance)) {
            // new Static 的意思就是创建它的调用者(被trait 复用的那个类)
            //学名延迟绑定
            self::$instance = new Static(...$param);
        }
        return self::$instance;
    }
}

class TaskManager
{
    use Singleton;

    private $taskName;

    function __construct(string $taskName = "")
    {
        $this->taskName = $taskName;
    }

    function getTask()
    {
        echo "i am " . $this->taskName;
    }
}

class ProcessManager
{
    use Singleton;

    function getProcess()
    {
        echo "i am process";
    }
}

class Config
{
    
    use Singleton;

    private $config = [];

    private $other;

    function __construct(array $config = [], String $other = "")
    {
        $this->config = $config;
        $this->other = $other;
    }

    function getData()
    {
        var_dump($this->config);
        var_dump($this->other);
    }
}

var_dump(TaskManager::getInstance("task1")); // object(TaskManager)#1 (1) {["taskName":"TaskManager":private]=>string(5) "task1"}
TaskManager::getInstance()->getTask(); // i am task1
               
// 由于单例已经创建 再次传递参数数无用的
TaskManager::getInstance("task2")->getTask(); // i am task1

var_dump(ProcessManager::getInstance()); // object(ProcessManager)#2 (0) {}
ProcessManager::getInstance()->getProcess(); // i am processs
                                             
// 给单例传递参数

Config::getInstance([
    [
        1,
        2,
        3
    ]
], "1231")->getData();

// array(1) {
// [0]=>
// array(3) {
// [0]=>
// int(1)
// [1]=>
// int(2)
// [2]=>
// int(3)
// }
// }
// string(4) "1231"

// 由于单例已经创建 再次传递参数数无用的

Config::getInstance()->getData();

// string(4) "1231"
// array(1) {
//     [0]=>
//     array(3) {
//         [0]=>
//         int(1)
//         [1]=>
//         int(2)
//         [2]=>
//         int(3)
//     }
// }

// 这里面 加入一个知识点 new self() 和 new static() 的区别

// 1、new static()是在php5.3版本引入的新特性
// 2、无论是 new static 还是 new self() 都是 new 一个对象
// 3、这两个方法new 出来的对象 有什么区别呢?说白了就是new出来的到底是同一个类的实列还是不同类的实列
class Father
{

    public function getNewFather()
    {
        return new self();
    }

    public function getNewCaller()
    {
        return new static();
    }
}

$f = new Father();

var_dump(get_class($f->getNewFather())); // Father
var_dump(get_class($f->getNewCaller()));

// Father

// 这里无论是getNewFather还是getNewCaller都是返回的 Father 这个实列
// 到这里貌似 new self() 还是 new static() 是没有区别的 我们接着走
class Sun1 extends Father
{
}

$sun1 = new Sun1();

var_dump($sun1->getNewFather()); // object(Father)#4 (0) { }
var_dump($sun1->getNewCaller()); // object(Sun1)#4 (0) { }


//这里我们发现了 getNewFather 返回的是Father的实列,
//而getNewCaller 返回的是调用者的实列

//现在明白了 new self() 和 new static 的区别了

//他们的区别只有在继承中才能体现出来、如果没有任何继承、那么二者没有任何区别

//然后 new self() 返回的实列是不会变的,无论谁去调用,都返回的一个类的实列,
//而 new static则是由调用者决定的。

最后修改:2019 年 09 月 02 日
如果觉得我的文章对你有用,请随意赞赏