Home > php教程 > PHP源码 > PHP单态的使用

PHP单态的使用

PHP中文网
Release: 2016-05-26 08:19:42
Original
1126 people have browsed it

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

<?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);
Copy after login


Related labels:
php
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template