首页 技术 正文
技术 2022年11月13日
0 收藏 519 点赞 5,013 浏览 3984 个字

模块的定义就不写了,直接进入主题看目录和文件:

application/modules/client/controllers/UserController.php

 <?php class UserController extends ClientController
{
public function init()
{
parent::init();
} public function actionIndex()
{
$userid = $this->user->userid;
$test = $this->user->test;
$test1 = $this->user->test();
exit('xx');
}

$this->user找不到,它会去ClientController中找

application/modules/client/components/ClientController.php

 <?php
class ClientController extends CController
{
public function init()
{
header('Content-type:text/html;charset=utf-8');
Yii::import('ext.functions', true); //加载公共函数
//$this->getUserId();
}
public function __get($name)
{
return Yii::app()->client->$name;
}

找到__get($name)方法,返回的是全局的client组件

application/config/main.php

 <?php …… 'components'=>array(
'client' => array(
'class' => 'application.modules.client.components.Client',
),
……
),

配置好client组件,然后指定到

application/modules/client/components/Client.php
 <?php use \Yii; class Client extends ApiComponent
{ private static $_params; private $_components; private $_propertys; private static $instance; public function __construct()
{
self::$instance =& $this;
} public static function &getInstance()
{
return self::$instance;
} public function init()
{ } /**
* 是否是组件
*
* @param string $id
* @return bool
*/
public function hasComponent($id)
{
return isset(self::$_coreComponents[$id]);
} /**
* 获取组件
* @param string $id
*/
public function getComponent($id)
{
if (isset($this->_components[$id]))
{
return $this->_components[$id];
} if (isset(self::$_coreComponents[$id]))
{
$component = Yii::createComponent(self::$_coreComponents[$id]);
$component->owner = $this;
$component->init();
$this->_components[$id] = $component;
return $component;
}
} /**
*
* @param integer $id
*/
public function HasProperty($id)
{
return isset(self::$_propertysMap[$id]);
} /**
* 获取属性
* @param string $id
*/
public function getProperty($id)
{
if (isset($this->_propertys[$id]))
{
return $this->_propertys[$id];
} if (isset(self::$_propertysMap[$id]))
{
list($component, $key) = explode('.', self::$_propertysMap[$id]);
$this->_propertys[$id] = $this->$component->$key;
}
return $this->_propertys[$id];
} /**
* 设置属性
*/
public function setProperty($id, $value)
{
if (isset(self::$_propertysMap[$id]))
{
list($component, $key) = explode('.', self::$_propertysMap[$id]);
$this->$component->$key = $value;
}
} /**
* 清除缓存
* @param string $id
*/
public function clearProperty($id)
{
unset($this->_propertys[$id]);
} /**
* (non-PHPdoc)
* @see company\components.CompanyComponent::__get()
*/
public function __get($name)
{
//组件
if ($this->hasComponent($name))
{
return $this->getComponent($name);
} //属性
if ($this->HasProperty($name))
{
return $this->getProperty($name);
} return parent::__get($name);
} /**
*
* @param unknown_type $name
* @param unknown_type $value
*/
public function __set($name, $value)
{
//属性
if ($this->HasProperty($name))
{
return $this->setProperty($name, $value);
} return parent::__set($name, $value);
} public function __call($name, $parameters)
{
return parent::__call($name, $parameters);
} private static $_coreComponents = array(
'user' => array(
'class' => 'application.modules.client.components.User'
),
); private static $_propertysMap = array(
'userid' => 'user.userid',
); }
可以从中看到定义好的user组件以及快捷获取user组件下面的userid属性
application/modules/client/components/user.php
 <?php class User extends ApiComponent
{
function getXzcoin()
{ } function getTest()
{
var_dump('safafaf');
return '1'; } function Test($name = 0){
echo $name; return 'test';
} function getUserid(){
echo '222';
return '2';
}
}
以及用到的application/modules/client/components/ApiComponent.php
 <?php use \CComponent; abstract class ApiComponent extends CComponent
{ public $owner; protected $_is; protected $_g; protected $_cacheGetter = TRUE; public function init()
{ } /**
*
* @param unknown_type $name
*/
public function __get($name)
{
if (strncasecmp($name, 'is', 2) === 0 && method_exists($this, $name))
{
$name = strtolower($name);
if ( ! isset($this->_is[$name]))
{
$this->_is[$name] = $this->$name();
}
return $this->_is[$name];
} $getter = 'get' . ucfirst($name); if (method_exists($this, $getter))
{
if ($this->_cacheGetter)
{
if (isset($this->_g[$name]))
{
return $this->_g[$name];
}
$this->_g[$name] = $this->$getter();
return $this->_g[$name];
} return $this->$getter();
} return parent::__get($name);
} /**
*
* @throws \Exception
*/
public function throwException($message, $code = 0)
{
throw new \Exception($message, $code);
} }

完毕。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,494
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295