php實現(xiàn)singleton()單例模式實例
更新時間:2014年11月06日 09:57:31 投稿:shichen2014
這篇文章主要介紹了php實現(xiàn)singleton()單例模式的方法,以實例形式簡單講述了單例模式的實現(xiàn)過程,需要的朋友可以參考下
本文實例講述了php實現(xiàn)singleton()單例模式的方法。分享給大家供大家參考。具體實現(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;
}
}
?>
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>測試</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>
<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程序設(shè)計有所幫助。
相關(guān)文章
從PHP $_SERVER相關(guān)參數(shù)判斷是否支持Rewrite模塊
這篇文章主要介紹了如何通過判斷PHP $_SERVER相關(guān)參數(shù)來驗證頁面是否重定向,但這只是在Apache環(huán)境下才有效,下面來具體的看下2013-09-09