PHP Object chain Operation Implementation Principle Analysis _php Skill

Source: Internet
Author: User
Tags strlen trim

This article illustrates the implementation principle of chain operation of PHP object. Share to everyone for your reference, specific as follows:

What is a chained operation? The students who use jquery should be impressed. In jquery, we often manipulate DOM elements like this:

$ ("P"). CSS ("color"). AddClass ("selected");

Coherent operations look really cool and very handy for code reading. So is it possible to implement it in PHP? The answer is yes, of course, must be used in OOP, in the process of the program, 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 space on either side of the string and then output its length, so you can use chained programming to:

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

Is it more comfortable to look at? This is mainly the use of PHP object-oriented inside the __call () and __tostring () Magic method

/** * Object Chain operation * 2015-04-24/class basechainobject{/** * Retrospective data, used for debugging * @var array/private $_trace_data = Arra
    Y ();
    /** * Save available methods list * @param array/protected $_methods = Array ();
    /** * Processing data * @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;
    }
    /*** Determine if the method exists * @param string/Private Function Vaild_func ($FN) {if!in_array ($FN, $this->_met
        Hods)) {throw new Exception ("Unvaild method");
    The 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 you can see from the code above, when you invoke a method that does not exist in the object, the __call () Magic method is automatically triggered, followed by the Call_user_func () to perform the chained operation, which triggers ToString when the object is output () To output the desired result. Of course, there is a solution in the custom method to use return this, you can also implement the object chain operation, we can go to try.

More about PHP Interested readers can view the site topics: "PHP object-oriented Program Design Primer", "PHP basic Grammar Introductory Course", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Array" operation Skills Encyclopedia, " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips

I hope this article will help you with the PHP program design.

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.