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('AppCalssLoader::loader',false); $db = AppDb::getInstance(); AppDb::getInstance(); AppDb::getInstance();
结果
调用多次,只走了一次实例化
代码地址
git@github.com:brady-wang/laravel-learn.git