单例模式

Db.php

<?php
/**
 * Created by PhpStorm.
 * User: brady
 * Date: 2019/4/10
 * Time: 14:00
 */

namespace App;


class Db
{
    private static $instance;

    private function __construct()
    {

    }

    public static function getInstance()
    {
        var_dump(self::$instance);
        if(self::$instance == null){
            self::$instance = new self;
        }
        return self::$instance;
    }

    private function __clone()
    {
        // TODO: Implement __clone() method.
    }
}

  另外一个文件调用 

require_once 'App/CalssLoader.php';
spl_autoload_register('\App\CalssLoader::loader',false);


$db =  \App\Db::getInstance();
\App\Db::getInstance();
\App\Db::getInstance();

  结果

调用多次,只走了一次实例化

代码地址

 git@github.com:brady-wang/laravel-learn.git

posted @ 2019-04-10 14:10  brady-wang  阅读(228)  评论(0编辑  收藏  举报