当前位置:首页 > PHP教程 > PHP高级教程

php实现singleton()单例模式实例

本文实例讲述了php实现singleton()单例模式的方法。分享给大家供大家参考。具体实现方法如下:

common.php文件如下:

复制代码 代码如下:
<?php 
class cc 

private static $ins; 
public static function singleton() 
 { 
         if (!isset(self::$ins)){ 
            $c = __class__; 
            self::$ins = new $c; 
         } 
         return self::$ins; 
    } 
public function eventresult($id) 

return $id; 


?>

index.php文件如下:
复制代码 代码如下:
<html> 
    <head> 
        <title>测试</title> 
        <meta http-equiv="content-type" content="text/html; charset=utf-8"> 
    </head> 
    <body> 
<?php 
require 'common.php'; 
$objcc=cc::singleton(); 
$r=$objcc->eventresult(7); 
print_r($objcc); 
echo $r."</br>"; 
?> 
</body></html>

希望本文所述对大家的php程序设计有所帮助。


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!