In practical development, we often call third-party class libraries such as SOAP services. The use of these third-party components is not difficult, the most troublesome is the call, the general debugging means the most convenient is to record the log. Example: If you have the following third-party class libraries.
// filename: user.php class user{ // 得到用户信息 public function getInfo( $uid ){ } } |
The calling code written by the general programmer may be: include ' user.php '; $face = new User (); $uid = 100; Parameter Write log $info = $face->getinfo ($uid); Return the results of the write log here is a question, if you want to use a third-party interface a lot, then this way will be a nightmare of the beginning; Here I use a proxy proxy way, in the Ruby language there is a very professional noun AOP can describe this technique, that is, dynamic class enhancement method.
// 代理技术
class
VProxy{
// 单例,如果在复杂资源时比较有用,如SOAP/DB等
static
private
$_instance
=
array
();
/**
* 单例模式返回实现(推荐)
*
* @param string $model 接口模型名
* @return object
*/
static
public
function
getInstance(
$model
)
{
$model
=
strtolower
(
$model
);
if
(!isset(self::
$_instance
[
$model
])){
self::
$_instance
[
$model
] =
new
self(
$model
);
}
return
self::
$_instance
[
$model
];
}
/**
* 当前调用接口的实例
*
* @var unknown_type
*/
private
$_model
= null;
private
$_modelName
=
‘‘
;
/**
* 构造函数
*
* @param string $model 接口模型名
*/
public
function __construct(
$model
){
include_once
(
$model
.
‘.php‘
);
$this
->_modelName =
$model
;
$this
->_model =
new
$model
;
}
/**
* proxy核心方法
*
* @param string $functionName :方法名
* @param mixed $args :传给方法的参数
* @return unknown
*/
public
function
__call(
$functionName
,
$args
)
{
// 调用接口
$result
= call_user_func_array(
array
(
$this
->_model,
$functionName
),
$args
));
// 写日志
writeLog(
$this
->_modelName,
$functionName
,
$args
,
$result
);
// 返回结果
return
$result
;
}
}
// 调用实例
$face
= VProxy::getInstance(
‘user‘
);
$info
=
$face
->getInfo(100);
|
The real example is just a finishing touch with, concrete implementation of the application than this complexity, the use of the above technology can also add a related method for the interface, this is similar to the characteristics of the object in JavaScript, specifically try it yourself! I often use this technology in thunder specific projects, such as the provision of public service interfaces for departments.
Transferred from: http://www.vquickphp.com/?a=blogview&id=25
The agent Technology of PHP development mode