The agent Technology of PHP development mode

Source: Internet
Author: User

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.phpclassuser{    // 得到用户信息    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.

// 代理技术classVProxy{     // 单例,如果在复杂资源时比较有用,如SOAP/DB等    staticprivate$_instance= array();         /**     * 单例模式返回实现(推荐)     *     * @param string $model 接口模型名     * @return object     */    staticpublicfunctiongetInstance($model)    {        $model= strtolower($model);        if(!isset(self::$_instance[$model])){            self::$_instance[$model] = newself($model);        }        returnself::$_instance[$model];    }     /**     * 当前调用接口的实例     *     * @var unknown_type     */    private$_model= null;    private$_modelName= ‘‘;     /**     * 构造函数     *     * @param string $model 接口模型名     */    publicfunction __construct($model){        include_once($model.‘.php‘);        $this->_modelName = $model;        $this->_model = new$model;    }     /**     * proxy核心方法     *     * @param string $functionName :方法名     * @param mixed $args :传给方法的参数     * @return unknown     */    publicfunction__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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.