PHP5 magic function, Magic constants

Source: Internet
Author: User
Tags constant contains include lowercase
Php5.0 has provided us with a lot of object-oriented features since it was released, especially to provide us with a lot of easy-to-use magic methods that allow us to simplify our coding and better design our systems. Today we come to know the Magic method that php5.0 offers us.

Magic function

1. __construct ()

is invoked when the object is instantiated,

When __construct and a function with the class name as the function name exist at the same time, the __construct is invoked and the other is not invoked.

2. __destruct ()

Called when an object is deleted or an object operation terminates.

3. __call ()

object to invoke a method,

If the method exists, it is called directly;

If it does not exist, it will call the __call function.

4. __get ()

When you read an object's properties,

If the attribute exists, the property value is returned directly;

If it does not exist, the __get function is called.

5. __set ()

When you set the properties of an object,

If the attribute exists, the value is directly assigned;

If it does not exist, the __set function is called.

6. __tostring ()

When an object is printed, it is invoked. such as Echo $obj, or print $obj;

7. __clone ()

Called when the object is cloned. such as: $t =new Test (); $t 1=clone $t;

8. __sleep ()

Serialize before being invoked. If the object is relatively large, want to cut a little bit of the serialization, you can consider this function.

9. __wakeup ()

Unserialize is invoked to do initialization work on some objects.

10. __isset ()

Called to detect whether an object's properties exist. such as: Isset ($c->name).

11. __unset ()

Called when a property of an object is unset. such as: unset ($c->name).

12. __set_state ()

Called when the Var_export is invoked. Use the return value of the __set_state as the return value of the Var_export.

13. __autoload ()

When an object is instantiated, the method is invoked if the corresponding class does not exist.

Magic Constants

1. __line__

Returns the current line number in the file.

2. __file__

Returns the full path and file name of the file. If used in the include file, the include file name is returned. Since PHP 4.0.2, __file__ always contains an absolute path, and the previous version sometimes contains a relative path.

3. __function__

Returns the name of the function (PHP 4.3.0 new addition). From PHP 5 This constant returns the name (case-sensitive) of the function when it is defined. This value is always lowercase in PHP 4.

4. __class__

Returns the name of the class (PHP 4.3.0 new). From PHP 5 This constant returns the name of the class when it is defined (case-sensitive). This value is always lowercase in PHP 4.

5. __method__

Returns the method name of the class (PHP 5.0.0 new). Returns the name (case-sensitive) of the method when it is defined.

(1) The Magic method of first recognition

Php5.0 has provided us with a lot of object-oriented features since it was released, especially to provide us with a lot of easy-to-use magic methods that allow us to simplify our coding and better design our systems. Today we come to know the Magic method that php5.0 offers us.

3,__get () is invoked when an attempt is made to read an attribute that does not exist.

If you try to read a property that does not exist for an object, PHP will give you an error message. If you add a __get method to a class, and we can use this function to implement various operations like reflection in Java.

Class Test

{

Public Function __get ($key)

{

Echo $key. "does not exist";

}

}

$t = new Test ();

Echo $t->name;

It will output:

Name does not exist

4,__set () is invoked when an attempt is made to write a value to a property that does not exist. Class Test

{

Public Function __set ($key, $value)

{

Echo ' right '. $key. "Attached value". $value;

}

}

$t = new Test ();

$t->name = "Aninggo";

It will output:

Attach value to name Aninggo

5,__call () This method is invoked when an attempt is made to invoke a method that does not exist for an object. Class Test

{

Public Function __call ($Key, $Args)

{

echo "The {$Key} method you are calling does not exist. The parameters you pass in are: ". Print_r ($Args, true);

}

}

$t = new Test ();

$t->getname (Aning,go);

The program will output:

The GetName method you are calling does not exist. parameter is: Array

(

[0] => aning

[1] => Go

)

6,__tostring () is called when an object is printed

This method is similar to the Java ToString method, and when we print the object directly, the callback uses this function

Class Test

{

Public Function __tostring ()

{

Return "Print Test";

}

}

$t = new Test ();

Echo $t;

When the Echo $t is run, the $t->__tostring () is invoked, which outputs

Print Test

7,__clone () Called when the object is cloned

Class Test

{

Public Function __clone ()

{

echo "I was copied!";

}

}

$t = new Test ();

$t 1 = Clone $t;

Program output:

I was copied.




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.