<table cellspacing="0" cellpadding="0"><tr><td class="t_f" id="postmessage_59137"> <div class="blockcode"> <div id="code_AZ9"><ol> <li>class User {</li> <li> static function getInstance()</li> <li> {</li> <li> if (self::$instance == NULL) { // If instance is not created yet, will create it.</li> <li> self::$instance = new User();</li> <li> }</li> <li> return self::$instance;</li> <li> }</li> <li> private function __construct() </li> <li> // Constructor method as private so by mistake developer not crate</li> <li> // second object of the User class with the use of new operator</li> <li> {</li> <li> }</li> <li> private function __clone()</li> <li> // Clone method as private so by mistake developer not crate </li> <li> //second object of the User class with the use of clone.</li> <li> {</li> <li> }</li> <li> <li> function Log($str)</li> <li> { </li> <li> echo $str;</li> <li> }</li> <li> static private $instance = NULL;</li> <li>}</li> <li>User::getInstance()->Log("Welcome User");</li> </ol></div> <em onclick="copycode($('code_AZ9'));">复制代码</em> </div> <br> </td></tr></table> <div class="ptg mbm mtn"> PHP</div> <div id="comment_59137" class="cm"> </div> <div id="post_rate_div_59137"></div> <br><br>