Blogger Information
Blog 23
fans 0
comment 1
visits 15986
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php基础实战-设计模式与MVC初步了解(2018年9月7日))
大白鲸的博客
Original
633 people have browsed it
  1. 单例模式


    实例

    <?php
    //单例模式,一个类只容许被实例化一次
    //
    class Config1 {}
    
    $obj1 = new Config1();
    $obj2 = new Config1();
    var_dump($obj1 === $obj2);
    echo '<hr>';
    /**
    * 
    */
    class Config
    {
    	private static $instance = null;
    	//禁止从类的外部实例化对象
    	private function __construct()
    	{
    
    	}
    
    	//克隆方法也私有化
    	private function __clone()
    	{
    		
    	}
    	//外部只允许通过一个公共静态方法来创建实例
    	public static function getInstance()
    	{
    		if (self::$instance == null) {
    			self::$instance = new self();
    		}
    		//如果已经存在当前类的实例,返回当前类的实例
    		return self::$instance;
    	}
    	
    }
    $obj1 = Config::getInstance();
    $obj2 = Config::getInstance();
    var_dump($obj1,$obj2);
    var_dump($obj1 === $obj2);
    echo '<hr>';

    运行实例 »

    点击 "运行实例" 按钮查看在线实例

  2. .MVC原理


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post