搞定,吃饭
<?php /* The purpose of singleton pattern is to restrict instantiation of class to a single object. It is implemented by creating a method within the class that creates a new instance of that class if one does not exist. If an object instance already exists, the method simply returns a reference to an existing object. */ class Logger { private static $instance; public static function getInstance() { if (!isset(self::$instance)) { self::$instance = new self; } return self::$instance; } public function logNotice($msg) { return 'logNotice: ' . $msg . '<br/>'; } public function logWaring($msg) { return 'logWaring: ' . $msg . '<br/>'; } public function logError($msg) { return 'logError: ' . $msg . '<br/>'; } } echo Logger::getInstance()->logNotice('test-log-notice'); echo Logger::getInstance()->logWaring('test-log-waring'); echo Logger::getInstance()->logError('test-log-error'); ?>

324

被折叠的 条评论
为什么被折叠?



