PHP magic function learning and application

Source: Internet
Author: User
(1) first knowledge of magic methods
Php5.0 has provided us with many features since its release. Object Features, especially providing us with a lot of easy-to-use magic methods that can simplify our coding and better design our system. Today, let's get to know the magic methods provided by php5.0.

1 ,__ construct () when instantiating an object, this method of this object is called first.

 
Class Test
{
Function _ construct ()

{
Echo "before ";
}
}

$ T = new test ();

The output is:

Start
We know that the PhP5 object model and the function with the same class name are class constructor. If we define the constructor and the _ construct () method at the same time, phP5 calls the constructor by default instead of the _ construct () function. Therefore, _ construct () is the default constructor of the class.

2 ,__ destruct () This method is called when an object or object operation is terminated.

 
Class Test
{
Function _ destruct ()
{
Echo "end ";
}
}
$ T = new test ();
Will output
End

We can release resources at the end of the object operation.

3 ,__ get () is called when an attempt is made to read an attribute that does not exist.
If you try to read an attribute that does not exist in an object, PHP will give an error message. If the _ Get method is added to the class, and we can use this function to implement operations similar to reflection in Java.

 
Class Test
{
Public Function _ Get ($ key)
{
Echo $ key. "nonexistent ";
}
}

$ T = new test ();
Echo $ T-> name;

The output is as follows:
Name does not exist

4 ,__ set () is called when you try to write a value to an attribute that does not exist.

 
Class Test
{
Public Function _ set ($ key, $ value)
{
Echo 'to'. $ key. "value". $ value;
}
}

$ T = new test ();
$ T-> name = "aninggo ";

The output is as follows:
NAME value aninggo

5 ,__ call () This method is called when you try to call a method that does not exist in an object.
Class Test
{
Public Function _ call ($ key, $ ARGs)
{
Echo "The {$ key} method you want to call does not exist. The parameter you passed in is: ". print_r ($ ARGs, true );
}
}

$ T = new test ();
$ T-> getname (aning, go );

 

ProgramThe output is as follows:
The getname method you want to call does not exist. The parameter is array.
(
[0] => aning
[1] => go
)

6 ,__ tostring () is called when an object is printed
This method is similar to the tostring method of Java. We call this function when we print the object directly.
Class Test
{
Public Function _ tostring ()
{
Return "Print test ";
}
}

$ T = new test ();

Echo $ t;

 

When Echo $ t; is run, $ T->__ tostring () is called to output
Print Test

7 ,__ clone () is called when the object is cloned
Class Test
{

Public Function _ clone ()
{
Echo "I have been copied! ";
}
}

$ T = new test ();
$ T1 = clone $ t;

Program output:
I have been cloned!

 

8. By the way, we will introduce several cool experimental functions provided by PhP5.
(1 ). Runkit_method_rename
This function dynamically changes the name of the called function.

 
Class Test
{

Function Foo (){
Return "foo! ";
}

}

Runkit_method_rename (
'Test', // Class Name
'Foo', // actually called Function
'Bar' // display the called Function
);

Echo test: bar ();

The program will output

Foo!

 

(2) runkit_method_add

This function can dynamically add functions to the class.

 
Class Test
{

Function Foo (){
Return "foo! ";
}

}

Runkit_method_add (
Test, // Class Name
'Add', // new function name
'$ Num1, $ num2', // input parameters
'Return $ num1 + $ num2; ', // executedCode
Runkit_acc_public
);

// Call
Echo $ e-> Add (12, 4 );

 

(3) runkit_method_copy
You can copy the functions in Class A to Class B and rename the functions.

 
Class Foo {
Function example (){
Return "foo! ";
}
}

Class bar {
// Empty class
}

// Execute copy
Runkit_method_copy ('bar', 'baz', 'foo', 'example ');

// Execute the copied Function
Echo bar: Baz ();

 

(4) runkit_method_redefine
Dynamically modify the return value of a function
This function allows us to easily implement mock testing for classes! Is it cool?

 
Class Example {
Function Foo (){
Return "foo! ";
}
}

// Create a test object
$ E = new example ();

// Output before the test object
Echo "before:". $ e-> Foo ();

// Modify the return value
Runkit_method_redefine (
'Example ',
'Foo ',
'',
'Return "bar! ";',
Runkit_acc_public
);

// Execution output
Echo "after:". $ e-> Foo ();

 

(5) runkit_method_remove
This function is very simple. You can see it by name and dynamically remove the function from the class.
Class test {
Function Foo (){
Return "foo! ";
}

Function bar (){
Return "bar! ";
}
}

// Remove the foo Function
Runkit_method_remove (
'Test ',
'Foo'
);

Echo implode ('', get_class_methods ('test '));

Program output
Bar

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.