這篇文章主要介紹了php實(shí)現(xiàn)singleton()單例模式的方法,以實(shí)例形式簡(jiǎn)單講述了單例模式的實(shí)現(xiàn)過程,需要的朋友可以參考下
本文實(shí)例講述了php實(shí)現(xiàn)singleton()單例模式的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
common.php文件如下:
復(fù)制代碼代碼如下:
<?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文件如下:
復(fù)制代碼代碼如下:
<html>
<head>
<title>測(cè)試</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>
希望本文所述對(duì)大家的PHP程序設(shè)計(jì)有所幫助。