Blogger Information
Blog 38
fans 1
comment 0
visits 26084
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
单例模式和工厂模式以及MVC的实现原理--2018年09月08日15时00分
一根火柴棒的博客
Original
641 people have browsed it

1.编程: 单例模式:

实例

<?php
//1.编程: 单例模式

echo '//1.编程: 单例模式','<hr>';

class Student {
    //private $name;
    //private $age;

    private static $instance = null;

    //private 外部就没办法实例化了
    private function __construct()
    {
        //$this->name = $name;
        //$this->age = $age;
    }

    //克隆方法私有化,外部也就无法访问了
    private function __clone()
    {
        // TODO: Implement __clone() method.
    }

    //外部仅通过公共的静态方法创建实例
    public static function getInstance()
    {
        //检测当前类的属性是否已经保存了当前类的实例
        if (self::$instance == null)
        {
            self::$instance == new self();
        }

        //返回当前的实例
        return self::$instance;
    }

}

$obj1 = Student::getInstance();
$obj2 = Student::getInstance();

var_dump($obj1,$obj2);
echo '<br>';
var_dump($obj1 === $obj2);
echo '<br><br>';

运行实例 »

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

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