国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

php中注冊器模式類用法實例分析

2024-05-04 23:40:04
字體:
供稿:網(wǎng)友

這篇文章主要介紹了php中注冊器模式類用法,以實例形式分析了注冊器讀寫類的相關(guān)使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了php中注冊器模式類用法。分享給大家供大家參考,具體如下:

注冊器讀寫類

Registry.class.php

 

 
  1. <?php 
  2. /**  
  3. * 注冊器讀寫類  
  4. */ 
  5. class Registry extends ArrayObject 
  6. /**  
  7. * Registry實例 
  8. * 
  9. * @var object  
  10. */ 
  11. private static $_instance = null
  12. /** 
  13. * 取得Registry實例 
  14.  
  15. * @note 單件模式 
  16.  
  17. * @return object 
  18. */ 
  19. public static function getInstance() 
  20. if (self::$_instance === null) { 
  21. self::$_instance = new self(); 
  22. echo "new register object!"
  23. return self::$_instance; 
  24. /** 
  25. * 保存一項內(nèi)容到注冊表中 
  26.  
  27. * @param string $name 索引 
  28. * @param mixed $value 數(shù)據(jù) 
  29.  
  30. * @return void 
  31. */ 
  32. public static function set($name, $value) 
  33. self::getInstance()->offsetSet($name, $value); 
  34. /** 
  35. * 取得注冊表中某項內(nèi)容的值 
  36.  
  37. * @param string $name 索引 
  38.  
  39. * @return mixed 
  40. */ 
  41. public static function get($name) 
  42. $instance = self::getInstance(); 
  43. if (!$instance->offsetExists($name)) { 
  44. return null
  45. return $instance->offsetGet($name); 
  46. /** 
  47. * 檢查一個索引是否存在  
  48.  
  49. * @param string $name 索引 
  50.  
  51. * @return boolean 
  52. */ 
  53. public static function isRegistered($name) 
  54. return self::getInstance()->offsetExists($name); 
  55. /** 
  56. * 刪除注冊表中的指定項 
  57.  
  58. * @param string $name 索引 
  59.  
  60. * @return void 
  61. */ 
  62. public static function remove($name) 
  63. self::getInstance()->offsetUnset($name); 

需要注冊的類

test.class.php

 

 
  1. <?php 
  2. class Test 
  3. function hello() 
  4. echo "hello world"
  5. return
  6. }  
  7. ?> 

測試 test.php

 

 
  1. <?php 
  2. //引入相關(guān)類 
  3. require_once "Registry.class.php"
  4. require_once "test.class.php"
  5. //new a object 
  6. $test=new Test(); 
  7. //$test->hello(); 
  8. //注冊對象 
  9. Registry::set('testclass',$test); 
  10. //取出對象 
  11. $t = Registry::get('testclass'); 
  12. //調(diào)用對象方法 
  13. $t->hello(); 
  14. ?> 

希望本文所述對大家php程序設(shè)計有所幫助。


注:相關(guān)教程知識閱讀請移步到PHP教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 昌黎县| 宁晋县| 汽车| 武汉市| 精河县| 沙雅县| 大城县| 自治县| 龙陵县| 上栗县| 延安市| 察哈| 浑源县| 通州市| 深圳市| 崇左市| 衡山县| 山丹县| 贵港市| 甘德县| 会理县| 夏邑县| 新乐市| 新邵县| 德保县| 红安县| 吉木乃县| 托克逊县| 托里县| 花莲县| 府谷县| 镇赉县| 洞头县| 巴林左旗| 沂南县| 巴林右旗| 姜堰市| 马龙县| 登封市| 平舆县| 来安县|