Magic Methods in PHP (Magic function)

Source: Internet
Author: User

I'll explain all the magic methods in PHP below. In fact, most of the following methods, for a qualified PHP programmer, are very familiar with things. I am here to make a summary, I make a record of learning, but also not familiar with these methods of the students to make an introduction. If there are omissions and errors, I hope you can point out. As mentioned below, most of the PHP5 are added magic methods, a small number in the PHP4 already exist, a small part of the PHP5.1.0 after the appearance, which I will be mentioned below.

__construct () and __destruct ()
Constructors __construct () and Destructors __destruct (), these two don't need me to say more, it's a programmer knows. __construct () is invoked when the instance is created, and __destruct () is invoked when the instance is destroyed. When you need attention, even if you don't explicitly call unset to destroy an instance, it will be destroyed at the end of the script run. Both of these methods can pass 0 or more parameters.

<?phpclass o{    public  function __construct ()    {    	echo ' building instance ';    }    Public  function __destruct ()    {    	echo ' destroy instance ';    }} $i = new O;unset ($i);//This sentence has no effect on the final output. __destruct () will always be invoked//output//building Instance//destroy instance?>

__get, __set, __isset, __unset
When a property is manipulated, one of the above methods is called if the property is not available (does not exist, or if the current environment cannot call the property, such as calling a private property of a parent class in a subclass). What method is invoked is based on the action on the property. Note: In PHP 5.0.*, these 4 methods must be public.
__get
Trigger Event: Call Property
Parameter 1: The name of the property being invoked
Example:

<?phpclass o{     protected function __get ($var)      {         return  ' No member: '. $var;     }} $i =new O (); Echo $i->name; Output No member:name?>

<?phpclass o{     Private   $name = ' surfchen '; note this private     protected function __get ($var)      {         Return  ' No: '. $var;     }} Class Child extends o{	function test ()	{		echo $this->name;	}} $c =new Child (); Echo $c->test (); Output No member:name?>

__set
Triggering event: assigning to a property
Parameter 1: The name of the property being invoked
Parameter 2: The value given
Example:

<?phpclass o{	protected function __set ($var, $val)	{		echo ' setting '. $var. ' to '. $val. '
";	}} $c =new O (); Echo $c->name= ' Wolfzeus '; /* Output: Setting Name to Wolfzeuswolfzeus*/?>

__isset
Trigger event: Detects whether a property exists with Isset
Parameter 1: The name of the property being invoked
Example:

<?phpclass o{	protected function __isset ($var)	{		echo $var. ' Have not been set ';	}} $c =new O (); Isset ($c->name); /* Output: Name have not been set*/?>

__unset
Trigger event: Unset an attribute (whether or not this property exists)
Parameter 1: The name of the property being invoked
Example:

<?phpclass o{	protected function __unset ($var)	{		echo $var. ' is unset ';	}} $c =new O (); unset ($c->name); /* Output: Name is unset*/?>

__call ($func, $para)
When an attempt is to invoke a method of an object, if the method does not exist, the __call ($func, $para) method is invoked. This method must have two parameters, the first is the calling method name, and the second is an array of arguments for the called method.

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.