PHP interceptor instance analysis, PHP interceptor instance analysis
This example describes the PHP interceptor usage. Share it with you for your reference. The details are as follows:
PHP provides several interceptors for calling when accessing undefined methods and attributes, as shown below:
1. _ get ($ property)
Function: the undefined access property is called.
2. _ set ($ property, $ value)
Function: called when setting undefined attribute values
3. _ isset ($ property)
Function: called when isset () is called for undefined attributes.
4. _ unset ($ property)
Function: called when unset () is called for undefined attributes.
5. _ call ($ method, $ arg_array)
Function: called when an undefined method is called.
The following uses a small program to describe the purpose of these interceptors:
Copy codeThe Code is as follows: class intercept_demo {
Private $ xingming = "";
Private $ age = 10;
// If an undefined property is accessed, the method corresponding to get {$ property} is called.
Function _ get ($ property ){
$ Method = "get {$ property }";
If (method_exists ($ this, $ method )){
Return $ this-> $ method ();
}
}
// If you set an undefined property value, the method corresponding to set {$ property} is called.
Function _ set ($ property, $ value ){
$ Method = "set {$ property }";
If (method_exists ($ this, $ method )){
Return $ this-> $ method ($ value );
}
}
// If you call the isset Method for undefined attributes,
Function _ isset ($ property ){
$ Method = "isset {$ property }";
If (method_exists ($ this, $ method )){
Return $ this-> $ method ();
}
}
// If you call the unset Method for undefined attributes,
// Call the corresponding unset {$ property} Method
Function _ unset ($ property ){
$ Method = "unset {$ property }";
If (method_exists ($ this, $ method )){
Return $ this-> $ method ();
}
}
Function _ call ($ method, $ arg_array ){
If (substr ($ method, 0, 3) = "get "){
$ Property = substr ($ method, 3 );
$ Property = strtolower (substr ($ property, 0, 1). substr ($ property, 1 );
Return $ this-> $ property;
}
}
Function testIsset (){
Return isset ($ this-> Name );
}
Function getName (){
Return $ this-> xingming;
}
Function setName ($ value ){
$ This-> xingming = $ value;
}
Function issetName (){
Return! Is_null ($ this-> xingming );
}
Function unsetName (){
$ This-> xingming = NULL;
}
}
$ Intercept = new intercept_demo ();
Echo "set attribute Name to Li ";
$ Intercept-> Name = "Li ";
Echo "\ $ intercept-> Name = {$ intercept-> Name }";
Echo "isset (Name) = {$ intercept-> testIsset ()}";
Echo "";
Echo "Clear attribute Name value ";
Unset ($ intercept-> Name );
Echo "\ $ intercept-> Name = {$ intercept-> Name }";
Echo "";
Echo "calling undefined getAge functions ";
Echo "age = {$ intercept-> getAge ()}";
I hope this article will help you with PHP programming.
PHP interceptor implementation
There are several $ _ SERVER variables.
$ Url = 'HTTP: // '. $ _ SERVER ['HTTP _ host']. $ _ SERVER ['request _ URI'];
Echo $ url;
These two are what you want ..
What is the difference between interceptor and attribute public in php?
The interceptor has an irreplaceable function. The interceptor can collectively process and store variables, but the public attributes are not good. If you use common public attributes, your Object-oriented thinking is gone. Boss.