Home  >  Article  >  Backend Development  >  Simple usage of PHP single state mode

Simple usage of PHP single state mode

墨辰丷
墨辰丷Original
2018-05-30 17:40:271282browse

This article mainly introduces the simple usage of PHP monomorphic mode, and analyzes the definition and related usage skills of PHP monomorphic mode in the form of examples. Friends in need can refer to it

Monomorphic classes can only Class instantiated once

<?php
/*
作者 : shyhero
*/
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);

The above is the entire content of this article, I hope it will be helpful to everyone's learning.


Related recommendations:

phpQuestions frequently encountered in string reversal interviews

How does PHP get the first non-repeating character in the character stream

PHP uses one line of code to delete all files in the directory

The above is the detailed content of Simple usage of PHP single state mode. For more information, please follow other related articles on the PHP Chinese website!

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