Usage analysis of various interceptors in php classes, php class interception usage_PHP tutorial

WBOY
Release: 2016-07-13 10:15:24
Original
861 people have browsed it

Analysis of usage of various interceptors in php class, usage of php class interception

The examples in this article describe the usage of various interceptors in the PHP class. Share it with everyone for your reference. The specific usage analysis is as follows:

1. __get( $property ) is called when accessing undefined properties

Copy code The code is as follows:
class lanjie
{
Function __get($name)

echo $name." property not found! ";
}  
}

$ob = new lanjie();
echo $ob->g;

When we call the undefined property g of object $ob, the interceptor __get() method is called and "g property not found!" is output;

2. __set( $property , $value ) assigns a value when calling an undefined property

Copy code The code is as follows:
class person
{
private $_age;
private $_name;
Function __set($name, $value)

         $method = "set". ucfirst($name);
echo $method;
If(method_exists($this, $method) )
                                                                                   Return $this->$method( $value );
                                                                                                              }  
       
Function setName( $name )

            $this->_name = $name;                                If( !is_null($this->_name) )
                                                                                   $this->_name = strtoupper($this->_name);
                                                                                                              }  
Function setAge( $age )

            return $this->_age = (int)$age;                                 }  
}

$p = new person();
$p->name = 'bob';
print_r( array( $p ) );


Here we can clearly see that when assigning a value to an undefined 'name', "__set()" will be called

Others include __call(), __isset() , __unset();
The most useful and commonly used ones here are __call(), which is called when calling an existing method; __isset(), which is called when using the isset() function on an undefined attribute, and __unset, which is called when calling an undefined attribute. The defined number is called when using unset
I hope this article will be helpful to everyone’s PHP programming design.


Implementation of PHP interceptor

Just a few $_SERVER variables

$url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $url;
This Two is what you want..



What is the difference between interceptors and public attributes in php

Interceptors have irreplaceable functions. Interceptors can collectively process and store variables, but public attributes are not good. If you use public attributes frequently, your object-oriented thinking will be gone. Boss.

http://www.bkjia.com/PHPjc/904926.html
www.bkjia.com

true

TechArticleAnalysis of usage of various interceptors in php class, php class interception usage This article describes the various interceptor usage in php class with examples Interceptor usage. Share it with everyone for your reference. The specific usage analysis is as follows: 1....
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