Application of PHP interceptor

Source: Internet
Author: User
The test on the use of PHP interceptor has finally ended. you can take a look at the code book with peace of mind. continue to learn PHPphp and provide the built-in interceptor, you can intercept messages sent to undefined methods and attribute messages. first look at _ get ($ property). it is called to access undefined attributes. See the following example: classCoder {& nbsp; function _ get ($ prop about PHP interceptor usage
The test is over, and you can read the code book with peace of mind.

Okay, continue learning PHP.

Php provides a built-in interceptor to intercept messages sent to undefined methods and attributes.

First look at _ get ($ property), which is called when accessing undefined properties.

See the following example:
Class Coder {
Function _ get ($ property ){
$ Method = "get {$ property }";
If (method_exists ($ this, $ method )){
Return $ this-> $ method ();
}
}

Function getName (){
Return "SUN ";
}
}

$ Coder = new Coder ();
Print $ coder-> name;

SUN will be output after the program is executed. if this method is not used in the program, nothing will be done and it will be parsed to NULL.

Similar methods include _ set () ,__ isset ,__ unset.

The following describes the _ call () method. it is called when an undefined method is called and has two parameters.
1. $ method. this is the method name;
2. $ arg_array: all parameters passed to the method to be called (array)

_ Call () example of delegate implementation

Class CoderWrite {
Function printName (Coder $ c ){
Print $ c-> getName ();
}
}

Class Coder {
Private $ write;
Function _ construct (CoderWrite $ cw ){
$ This-> write = $ cw;
}

Function _ call ($ methodname, $ args ){
If (method_exists ($ this-> write, $ methodname )){
Return $ this-> write-> $ methodname ($ this );
}
}

Function getName (){
Return "SUN ";
}
}

Call: $ coder = new Coder (new CoderWrite ());
$ Coder-> printName ();
The _ call () method will be called, and then find whether the printName () method in the CoderWrite object exists. If yes, it will be called. Is it a disguised method to add a Coder object?

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.