Application of PHP5 magic Functions

Source: Internet
Author: User

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

 
 
  1. class Test   
  2. {   
  3.  function __construct()   
  4.  {   
  5.   echo "before";   
  6.  }   
  7. }   
  8. $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.

PHP5 magic function 2 ,__ destruct () This method is called when an object is deleted or the object operation is terminated.

 
 
  1. class Test   
  2. {   
  3.  function __destruct()   
  4.  {   
  5.   echo "end";   
  6.  }   
  7. }   
  8. $t = new Test(); 

Will output

End

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

PHP5 magic function 3 ,__ get () is called when you try 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 various operations similar to reflection in java

 
 
  1. Class Test
  2. {
  3. Public function _ get ($ key)
  4. {
  5. Echo $ key. "nonexistent ";
  6. }
  7. }
  8.  
  9.  
  10. $T=NewTest ();
  11. Echo $ t->Name;
  12.  
  13. The output is as follows:
  14. Name does not exist

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

 
 
  1. Class Test
  2. {
  3. Public function _ set ($ key, $ value)
  4. {
  5. Echo 'to'. $ key. "value". $ value;
  6. }
  7. }
  8.  
  9.  
  10. $T=NewTest ();
  11. $ T->Name="Aninggo";
  12.  
  13. The output is as follows:
  14. Name value aninggo

PHP5 magic function 5 ,__ call () This method is called when an object does not exist.

 
 
  1. Class Test
  2. {
  3. Public function _ call ($ Key, $ Args)
  4. {
  5. Echo "The {$ Key} method you want to call does not exist. The parameter you passed in is: ". print_r ($ Args, true );
  6. }
  7. }
  8.  
  9. $T=NewTest ();
  10. $ T->GetName (aning, go );

The program will output:

The getName method you want to call does not exist. The parameter is Array.

(

[0] => aning

[1] => go

)

PHP5 magic function 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.

 
 
  1. Class Test
  2. {
  3. Public function _ toString ()
  4. {
  5. Return "Print Test ";
  6. }
  7. }
  8.  
  9.  
  10. $T=NewTest ();
  11.  
  12. Echo $ t;

When echo $ t; is run, $ t->__ toString () is called to output

Print Test

PHP5 magic function 7 ,__ clone () is called when an object is cloned.

 
 
  1. Class Test
  2. {
  3.  
  4. Public function _ clone ()
  5. {
  6. Echo "I have been copied! ";
  7. }
  8. }
  9.  
  10. $T=NewTest ();
  11. $T1=Clone$ T;
  12.  
  13. Program output:
  14. I have been cloned!

PHP5 magic functions 8. Some COOl experimental functions provided by php5

(1) runkit_method_rename

This function dynamically changes the name of the called function.

 
 
  1. Class Test
  2. {
  3.  
  4. Function foo (){
  5. Return "foo! ";
  6. }
  7.  
  8. }
  9.  
  10. Runkit_method_rename (
  11. 'Test', // Class Name
  12. 'Foo', // actually called Function
  13. 'Bar' // display the called Function
  14. );
  15.  
  16. Echo Test: bar ();
  17.  
  18. The program will output
  19. Foo!

(2) runkit_method_add

This PHP5 magic function can dynamically Add a function to the class.

 
 
  1. Class Test
  2. {
  3.  
  4. Function foo (){
  5. Return "foo! ";
  6. }
  7.  
  8. }
  9.  
  10. Runkit_method_add (
  11. Test, // Class Name
  12. 'Add', // new function name
  13. '$ Num1, $ num2', // input parameters
  14. 'Return $ num1 + $ num2; ', // code to be executed
  15. RUNKIT_ACC_PUBLIC
  16. );
  17.  
  18. // Call
  19. Echo $ e->Add (12, 4 );

(3) runkit_method_copy

You can copy the function in Class A to Class B and rename the PHP5 magic function.

 
 
  1. Class Foo {
  2. Function example (){
  3. Return "foo! ";
  4. }
  5. }
  6.  
  7. Class Bar {
  8. // Empty class
  9. }
  10.  
  11. // Execute copy
  12. Runkit_method_copy ('bar', 'baz', 'foo', 'example ');
  13.  
  14. // Execute the copied Function
  15. Echo Bar: baz ();

(4) runkit_method_redefine

Dynamically modify the return value of a function

This PHP5 magic function allows us to easily implement MOCK testing for classes! Is it COOL?

  1. Class Example {
  2. Function foo (){
  3. Return "foo! ";
  4. }
  5. }
  6. // Create a test object
  7. $E=NewExample ();
  8. // Output before the test object
  9. Echo "Before:". $ e->Foo ();
  10. // Modify the return value
  11. Runkit_method_redefine (
  12. 'Example ',
  13. 'Foo ',
  14. '',
  15. 'Return "bar! ";',
  16. RUNKIT_ACC_PUBLIC
  17. );
  18. // Execution output
  19. Echo "After:". $ e->Foo ();

(5) runkit_method_remove

This PHP5 magic function is very simple. You can see it by name and dynamically remove the function from the class.

 
 
  1. Class Test {
  2. Function foo (){
  3. Return "foo! ";
  4. }
  5. Function bar (){
  6. Return "bar! ";
  7. }
  8. }
  9.  
  10. // Remove the foo Function
  11. Runkit_method_remove (
  12. 'Test ',
  13. 'Foo'
  14. );
  15.  
  16. Echo implode ('', get_class_methods ('test '));
  17.  
  18. Program output
  19. Bar

The code described above is the specific application method of PHP5 magic functions.


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.