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

Analysis on the implementation principle of PHP object chain operation

高洛峰
Release: 2023-03-03 17:48:01
Original
1172 people have browsed it

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");
Copy after login

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));
Copy after login

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();
Copy after login

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();
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template