首页 > 技术文章 > Mage::register 函数详解:

tecliu 2014-03-20 17:53 原文

在Mage类中使用$_registry存储生成的对象,这样生成的对象在全局都可以访问。Mage::register 函数有两种格式:

 

  •  public static function register($key, $value, $graceful = false) 注册一个新的变量
  •  public static function registry($key) 通过$key获取已有变量
  •  public static function unregister($key) 注销一个变量

 

Magento源文件:

Php代码  收藏代码
  1. public static function register($key$value$graceful = false)  
  2. {  
  3. if (isset(self::$_registry[$key])) {  
  4. if ($graceful) {  
  5. return;  
  6. }  
  7. self::throwException('Mage registry key "'.$key.'" already exists');  
  8. }  
  9. self::$_registry[$key] = $value;  
  10. }  
  11. /** 
  12. * Retrieve a value from registry by a key 
  13. * 
  14. * @param string $key 
  15. * @return mixed 
  16. */  
  17. public static function registry($key)  
  18. {  
  19. if (isset(self::$_registry[$key])) {  
  20. return self::$_registry[$key];  
  21. }  
  22. return null;  
  23. }  

摘自:http://justcoding.iteye.com/blog/1572599

推荐阅读