Home  >  Article  >  Backend Development  >  Analysis on the implementation principle of PHP object chain operation

Analysis on the implementation principle of PHP object chain operation

高洛峰
高洛峰Original
2016-12-20 16:07:051129browse

The example in this article describes the implementation principle of PHP object chain operation. Share it with everyone for your reference, the details are as follows:

What is chain operation? Students who use jQuery should be very impressed. In jQuery, we often operate DOM elements like this:

$("p").css("color").addClass("selected");

The coherent operation looks really cool, and it is very convenient to read the code. So can it be implemented in PHP? The answer is yes, of course it must be used in OOP. In procedural programs, there is no need to use this method.

In PHP, we often use many functions:

$str = 'abs123 ';
echo strlen(trim($str));

The function of the above code is to remove the spaces on both sides of the string and then output its length. Then use chain programming to do it like this:

$str = 'abs123 ';
echo $str->trim()->strlen();

Is it more comfortable to look at? This mainly uses the __call() and __toString() magic methods in PHP object-oriented

class BaseChainObject{
    /**
* 追溯数据,用来进行调试
* @var array
*/
private $_trace_data = array();
    /**
    *    保存可用方法列表
    *    @param array
    */
    protected $_methods = array();
    /**
    *    处理的数据
    *    @param null
    */
    public $data;
    function __construct($data){
        $this->data = $data;
        $this->_trace_data['__construct'] = $data;
        return $this->data;
    }
    function __toString(){
        return (String)$this->data;
    }
    function __call($name,$args){
        try{
            $this->vaild_func($name);
        }catch(Exception $e){
            echo $e->getMessage();
            exit();
        }
        if (!$args) {
            $args = $this->data;
            $this->data = call_user_func($name,$args);
        }else{
            $this->data = call_user_func_array($name,$args);
        }
        $this->_trace_data[$name] = $this->data;
        return $this;
    }
    /**
    *    判断方法是否存在
    *    @param string
    */
    private function vaild_func($fn){
        if(!in_array($fn, $this->_methods)){
            throw new Exception("unvaild method");
        }
    }
    public function trace(){
      var_dump($this->_trace_data);
    }
}
class String extends BaseChainObject{
    protected $_methods = array('trim','strlen');
}
$str = new String('ab rewqc ');
echo $str->trim()->strlen();
$str->trace();

As can be seen from the above code, when calling When a method does not exist in the object, the __call() magic method will be automatically triggered, and then combined with call_user_func() to perform chain operations. When the object is output, toString() is triggered to output the desired result. Of course, there is another solution Just by using return this in a custom method, you can also implement object chain operations. You can try it yourself.

I hope this article will be helpful to everyone in PHP programming.

For more articles related to PHP object chain operation implementation principle analysis, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn