模块的定义就不写了,直接进入主题看目录和文件:
application/modules/client/controllers/UserController.php
1 <?php 2 3 class UserController extends ClientController 4 { 5 public function init() 6 { 7 parent::init(); 8 } 9 10 public function actionIndex() 11 { 12 $userid = $this->user->userid; 13 $test = $this->user->test; 14 $test1 = $this->user->test(); 15 exit('xx'); 16 }
$this->user找不到,它会去ClientController中找
application/modules/client/components/ClientController.php
1 <?php 2 class ClientController extends CController 3 { 4 public function init() 5 { 6 header('Content-type:text/html;charset=utf-8'); 7 Yii::import('ext.functions', true); //加载公共函数 8 //$this->getUserId(); 9 } 10 public function __get($name) 11 { 12 return Yii::app()->client->$name; 13 }
找到__get($name)方法,返回的是全局的client组件
application/config/main.php
1 <?php 2 3 …… 4 5 'components'=>array( 6 'client' => array( 7 'class' => 'application.modules.client.components.Client', 8 ), 9 …… 10 ),
配置好client组件,然后指定到
application/modules/client/components/Client.php
1 <?php 2 3 use Yii; 4 5 class Client extends ApiComponent 6 { 7 8 private static $_params; 9 10 private $_components; 11 12 private $_propertys; 13 14 private static $instance; 15 16 public function __construct() 17 { 18 self::$instance =& $this; 19 } 20 21 public static function &getInstance() 22 { 23 return self::$instance; 24 } 25 26 public function init() 27 { 28 29 } 30 31 32 /** 33 * 是否是组件 34 * 35 * @param string $id 36 * @return bool 37 */ 38 public function hasComponent($id) 39 { 40 return isset(self::$_coreComponents[$id]); 41 } 42 43 /** 44 * 获取组件 45 * @param string $id 46 */ 47 public function getComponent($id) 48 { 49 if (isset($this->_components[$id])) 50 { 51 return $this->_components[$id]; 52 } 53 54 if (isset(self::$_coreComponents[$id])) 55 { 56 $component = Yii::createComponent(self::$_coreComponents[$id]); 57 $component->owner = $this; 58 $component->init(); 59 $this->_components[$id] = $component; 60 return $component; 61 } 62 } 63 64 /** 65 * 66 * @param integer $id 67 */ 68 public function HasProperty($id) 69 { 70 return isset(self::$_propertysMap[$id]); 71 } 72 73 /** 74 * 获取属性 75 * @param string $id 76 */ 77 public function getProperty($id) 78 { 79 if (isset($this->_propertys[$id])) 80 { 81 return $this->_propertys[$id]; 82 } 83 84 if (isset(self::$_propertysMap[$id])) 85 { 86 list($component, $key) = explode('.', self::$_propertysMap[$id]); 87 $this->_propertys[$id] = $this->$component->$key; 88 } 89 return $this->_propertys[$id]; 90 } 91 92 /** 93 * 设置属性 94 */ 95 public function setProperty($id, $value) 96 { 97 if (isset(self::$_propertysMap[$id])) 98 { 99 list($component, $key) = explode('.', self::$_propertysMap[$id]); 100 $this->$component->$key = $value; 101 } 102 } 103 104 /** 105 * 清除缓存 106 * @param string $id 107 */ 108 public function clearProperty($id) 109 { 110 unset($this->_propertys[$id]); 111 } 112 113 /** 114 * (non-PHPdoc) 115 * @see companycomponents.CompanyComponent::__get() 116 */ 117 public function __get($name) 118 { 119 //组件 120 if ($this->hasComponent($name)) 121 { 122 return $this->getComponent($name); 123 } 124 125 //属性 126 if ($this->HasProperty($name)) 127 { 128 return $this->getProperty($name); 129 } 130 131 return parent::__get($name); 132 } 133 134 /** 135 * 136 * @param unknown_type $name 137 * @param unknown_type $value 138 */ 139 public function __set($name, $value) 140 { 141 //属性 142 if ($this->HasProperty($name)) 143 { 144 return $this->setProperty($name, $value); 145 } 146 147 return parent::__set($name, $value); 148 } 149 150 public function __call($name, $parameters) 151 { 152 return parent::__call($name, $parameters); 153 } 154 155 private static $_coreComponents = array( 156 'user' => array( 157 'class' => 'application.modules.client.components.User' 158 ), 159 ); 160 161 private static $_propertysMap = array( 162 'userid' => 'user.userid', 163 ); 164 165 }
可以从中看到定义好的user组件以及快捷获取user组件下面的userid属性
application/modules/client/components/user.php
1 <?php 2 3 class User extends ApiComponent 4 { 5 function getXzcoin() 6 { 7 8 } 9 10 function getTest() 11 { 12 var_dump('safafaf'); 13 return '1'; 14 15 } 16 17 function Test($name = 0){ 18 echo $name;19 return 'test'; 20 } 21 22 function getUserid(){ 23 echo '222'; 24 return '2'; 25 } 26 }
以及用到的application/modules/client/components/ApiComponent.php
1 <?php 2 3 use CComponent; 4 5 abstract class ApiComponent extends CComponent 6 { 7 8 public $owner; 9 10 protected $_is; 11 12 protected $_g; 13 14 protected $_cacheGetter = TRUE; 15 16 public function init() 17 { 18 19 } 20 21 /** 22 * 23 * @param unknown_type $name 24 */ 25 public function __get($name) 26 { 27 if (strncasecmp($name, 'is', 2) === 0 && method_exists($this, $name)) 28 { 29 $name = strtolower($name); 30 if ( ! isset($this->_is[$name])) 31 { 32 $this->_is[$name] = $this->$name(); 33 } 34 return $this->_is[$name]; 35 } 36 37 $getter = 'get' . ucfirst($name); 38 39 if (method_exists($this, $getter)) 40 { 41 if ($this->_cacheGetter) 42 { 43 if (isset($this->_g[$name])) 44 { 45 return $this->_g[$name]; 46 } 47 $this->_g[$name] = $this->$getter(); 48 return $this->_g[$name]; 49 } 50 51 return $this->$getter(); 52 } 53 54 return parent::__get($name); 55 } 56 57 /** 58 * 59 * @throws Exception 60 */ 61 public function throwException($message, $code = 0) 62 { 63 throw new Exception($message, $code); 64 } 65 66 }
完毕。