PHP5 magic functions-PHP Tutorial

Source: Internet
Author: User
Explain the specific application of PHP5 magic functions. In PHP5 magic function 1 ,__ construct () when instantiating an object, this method of this object is called first. ClassTest {function _ construct () {echobefore ;}}$ t in PHP5 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. $T=NewTest ();
  9. Echo $ t->Name;
  10. The output is as follows:
  11. 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. $T=NewTest ();
  9. $ T->Name="Aninggo";
  10. The output is as follows:
  11. 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. $T=NewTest ();
  9. $ 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. $T=NewTest ();
  9. 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. Public function _ clone ()
  4. {
  5. Echo "I have been copied! ";
  6. }
  7. }
  8. $T=NewTest ();
  9. $T1=Clone$ T;
  10. Program output:
  11. 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. Function foo (){
  4. Return "foo! ";
  5. }
  6. }
  7. Runkit_method_rename (
  8. 'Test', // class name
  9. 'Foo', // actually called function
  10. 'Bar' // display the called function
  11. );
  12. Echo Test: bar ();
  13. The program will output
  14.  
  15. Foo!

(2) runkit_method_add

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

 
 
  1. Class Test
  2. {
  3. Function foo (){
  4. Return "foo! ";
  5. }
  6. }
  7. Runkit_method_add (
  8. Test, // class name
  9. 'Add', // New function name
  10. '$ Num1, $ num2', // input parameters
  11. 'Return $ num1 + $ num2; ', // code to be executed
  12. RUNKIT_ACC_PUBLIC
  13. );
  14. // Call
  15. 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. Class Bar {
  7. // Empty class
  8. }
  9. // Execute copy
  10. Runkit_method_copy ('bar', 'Baz', 'foo', 'example ');
  11. // Execute the copied function
  12. 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. // Remove the foo function
  10. Runkit_method_remove (
  11. 'Test ',
  12. 'Foo'
  13. );
  14. Echo implode ('', get_class_methods ('test '));
  15. Program output
  16. Bar

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


PHP5 magic function 1 ,__ construct () when instantiating an object, this method of this object is called first. ClassTest {function _ construct () {echo "before" ;}}$ t =...

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.