php interceptor methods include: 1. "__get" method, 2. "__set()" method; 3. "__isset()" method; 4. "__unset($property)" method; 6. "__call()" method.
What interceptor methods does php provide?
Serial number | Method name | Function | Implementation version |
__get($property) | When accessing undefined properties, call this method | >= PHP 5.3.0 | |
__set($property, $value) | When setting a value for an undefined property, call this method | >= PHP 5.3.0 | |
__isset($property) | This method is called when calling isset() on an undefined property | > = PHP 5.1.0 | |
__unset($property) | This method is called when unset() is called on an undefined property | >= PHP 5.1.0 | |
##__call($property, $args_array) | When calling an undefined method, Execute this method | >= PHP 5.3.0 |
The English name is "interceptor", its function is to intercept messages sending undefined methods and attributes.
Let’s first look at a piece of code. A School class is defined, an object $obj is instantiated, and an undefined attribute teacher is obtained. What will happen?
<?php class School { } $obj = new School; var_dump($obj->teacher);
If you use the php command line to execute, you can see that php reported a Notice of undefined attributes, and the $obj->teacher value is a null value, so NULL
[root@localhost php]# php538 interceptor.php PHP Notice: Undefined property: School::$teacher in /usr/local/sina_mobile/apache/htdocs/php/interceptor.php on line 11NULL
is printed. Let me explain here that the php command line execution and the browser page execution are not necessarily exactly the same. We can see the screenshot below. When the web server executes the same script by apache, apache reports to the browser a Notice without php, which is returned to The browser's response is 200 OK. There are problems with the interaction between php and the web server and the interaction between the web server and the browser.
Regarding the previous code, we think that if the teacher attribute does not exist when accessing the teacher, we can intercept the call request to the teacher and do some operations, then This is the PHP interceptor. The PHP interceptor provides a variety of methods. The __get method here is called when accessing undefined attributes.
Let’s look at the following code and results
<?php class School { function __get($var) { echo $var; echo "\r\n"; return "hello"; } public $name = "Tom"; } $obj = new School; var_dump($obj->teacher); var_dump($obj->name);
The results of executing the following are as follows. To explain, the __get method must have a parameter, which is the undefined attribute name of the call. The function will return the result. The unknown property assigned to this call.
[root@localhost php]# php538 interceptor.php teacher string(5) "hello" string(3) "Tom"
For more related knowledge, please visit
PHP Chinese websiteThe above is the detailed content of What are the interceptor methods in php?. For more information, please follow other related articles on the PHP Chinese website!