Home  >  Article  >  php教程  >  PHP单态的使用

PHP单态的使用

PHP中文网
PHP中文网Original
2016-05-26 08:19:421102browse

单态类就是只能实例化一次的类

<?php
    /*
        作者 : shyhero
        邮箱 : shyhero@outlook.com
        Q  Q : 1757424878
     */
    class Demo{
        private static $ob=null;
 
        private $name;
        private $age;
 
        private function __construct(){
 
        }
 
        public static function makeOb(){
            var_dump($a);
            if(self::$ob == null){
                $c = __CLASS__;
                self::$ob = new $c();
            }
            return self::$ob;
        }
 
        public function __set($tName,$value){
            $this -> $tName = $value;
        }
    }
 
    $a = Demo::makeOb();
    var_dump($a);


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn