The specific application of PHP5 magic function explain _php tutorial

Source: Internet
Author: User
In PHP5 Magic Function 1,__construct () when an object is instantiated, this method of the 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 same class name function are the constructors of the class, so if we define both the constructor and the __construct () method, PHP5 will call the constructor by default instead of calling the __construct () function, so __construct () As the default constructor for a class

PHP5 Magic Function 2,__destruct () invokes the method when an object or object operation is terminated.

 
  
  
  1. Class Test
  2. {
  3. function __destruct ()
  4. {
  5. echo "End";
  6. }
  7. }
  8. $ T New

will be output

End

We can do things like freeing resources at the end of an object operation.

PHP5 Magic Function 3,__get () is called when attempting to read a property 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 actions like reflection in Java

 
  
  
  1. Class Test
  2. {
  3. Public Function __get ($key)
  4. {
  5. Echo $key. "does not exist";
  6. }
  7. }
  8. $ T New Test ();
  9. Echo $t- > name;
  10. It will output:
  11. Name does not exist

PHP5 Magic Function 4,__set () is called when attempting to write a value to a property that does not exist.

 
  
  
  1. class Test
  2. {
  3. public function __set ($key, $value)
  4. {
  5. Echo ' pair '. $key. "Attached value". $value;
  6. }
  7. }
  8. $ t = new Test ();
  9. $t- ; name = "Aninggo" ;
  10. output:
  11. to name appended value Aninggo

PHP5 Magic Function 5,__call () Call this method when attempting to invoke a method that does not exist for an object.

 
  
  
  1. Class Test
  2. {
  3. Public Function __call ($Key, $Args)
  4. {
  5. echo "The {$Key} method you are calling does not exist. The parameter you passed in is: ". Print_r ($Args, true);
  6. }
  7. }
  8. $ T New Test ();
  9. $t- > GetName (Aning,go);

The program will output:

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

(

[0] = aning

[1] = = Go

)

PHP5 Magic Function 6,__tostring () is called when printing an object

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

 
  
  
  1. Class Test
  2. {
  3. Public Function __tostring ()
  4. {
  5. Return "Print Test";
  6. }
  7. }
  8. $ T New Test ();
  9. Echo $t;

When the Echo $t is run, $t->__tostring () is called and the output

Print Test

PHP5 Magic Function 7,__clone () is called when the object is cloned

 
  
  
  1. Class Test
  2. {
  3. Public Function __clone ()
  4. {
  5. echo "I've been copied!" ";
  6. }
  7. }
  8. $ T New Test ();
  9. $ T1 clone $t;
  10. Program output:

PHP5 Magic function 8. By the way, some of the very cool experimental functions provided in PHP5 are

(1) runkit_method_rename

This function can dynamically change the name of the function we call.

 
  
  
  1. class Test
  2. {
  3. function foo () {
  4. return "Foo!";
  5. }
  6. }
  7. runkit_method_rename (
  8. < span> ' Test ',//class name
  9. ' foo ',//function actually called
  10. ' Ba R '//display called function
  11. Echo Test::bar ();
  12. program outputs
  13. foo!

(2) Runkit_method_add

This PHP5 magic function can dynamically add functions to a 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. ' $num 1, $num 2 ',//parameters passed in
  11. ' Return $num 1 + $num 2; ',//code executed
  12. Runkit_acc_public
  13. );
  14. Call
  15. Echo $e- > Add (4);

(3) runkit_method_copy

You can copy the functions 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. function after the copy is executed
  12. Echo Bar::baz ();

(4) Runkit_method_redefine

Dynamically changing the return value of a function

This PHP5 magic function allows us to easily implement mock tests of classes! Isn't it cool?

  1. class Example {
  2. function foo () {
  3. return "foo!";
  4. }
  5. }
  6. //Create a Test object
  7. $ e = New Example ();
  8. //output before testing the object
  9. echo "Before:". $e->foo ();
  10. //Modify 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, see the name can be seen, the dynamic removal of the function from the class

 
 
    1. class Test {
    2. function foo () {
    3. return "Foo!";
    4. }
    5. Fun Ction Bar () {
    6. return "bar!";
    7. }
    8. }
    9. li>
    10. //remove foo function
    11. Runkit_method_remove (
    12. ' Test ',
    13. ' foo '
    14. );
    15. Echo implode (', Get_class_methods (' Test '));
    16. program output
    17. bar

The code described above is about how the PHP5 magic function is applied.


http://www.bkjia.com/PHPjc/446384.html www.bkjia.com true http://www.bkjia.com/PHPjc/446384.html techarticle in PHP5 Magic function 1,__construct () When instantiating an object, this method of the object is called first. classtest {function__construct () {echo "Before";}} $ t = ...

  • Related Article

    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.