Tencent PHP programmer face Test-15 Summary of Magic Methods

Source: Internet
Author: User
    1. Function: Automatically loads files of this class when the current page is using a class

    2. Parameter $classname the name of the class can be customized

    3. */

    4. function __autoload ($className) {

    5. $className =strtolower ($className);//preferably with this step, because the class is capitalized and some system-sensitive file names are cased

    6. Require ("$className. class.php");//Load class file One.class.php Two.class.php in the same directory as this file

    7. Echo '------------------------------
      ';//As a token at the time of the call to test

    8. }

    9. The following two classes are not defined in this file

    10. $o =new One ();//Use the one class to initialize it

    11. $t =new ();//Use the class of both to initialize it

    12. ?>

Copy Code

Note that if the function needs to load normally, the file name needs to be set to the class name (lowercase is recommended). Class.php class names and filenames have some kind of association, and if the function has an exception that cannot be caught by catch, fatal Error will be displayed.

2.__construct () constructor

The function names of constructors in C # and PHP4 are the same as the class names, and of course PHP5 is supported (if the __construct method is found to be the same constructor as the class name), the advantage of this is that if we want to change the name of the class, we just change the class name. Instead of changing the function name of the constructor and, to some extent, reducing the probability of our error, the sample code is simply written!

    1. Class pcyoyo{

    2. Private $url;

    3. function __construct ($url) {

    4. $this->url= $url;

    5. }

    6. }

    7. ?>

Copy Code

3.__destruct () destructor

__destruct destructor, PHP calls this method before the object is destroyed (that is, before it is purged from memory)

By default, PHP simply frees the memory occupied by the object's properties and destroys the object-related resources.

Destructors allow you to execute arbitrary code after an object is used to clear memory.

When PHP decides that your script is no longer related to the object, the destructor is called.

Within the namespace of a function, this occurs at the time of the function return.

For global variables, this happens at the end of the script. If you want to explicitly destroy an object, you can assign any other value to the variable that points to the object. You typically assign a variable to be on duty to null or call unset.

4.__call () Called when the called member method does not exist (or does not have permission) for the class, to do some action or to prompt the message after the error

  1. Initialize a class

  2. Class person{

  3. Private $name;

  4. Private $age;

  5. Private $sex;

  6. function Walk ($hour, $minute) {

  7. echo "$hour: $minute We'll walk
    ";

  8. }

  9. function Run () {

  10. }

  11. Using the Magic Method __call

  12. /*****

  13. Function: Called automatically when an object calls a function that does not exist in the class, doing something

  14. Parameters: $proName The called function name $args the parameter array $proName, $args the custom parameter can conform to its own style, but the meaning of the two parameters is fixed

  15. *****/

  16. function __call ($proName, $args) {

  17. echo "The $proName doesn ' t exist! The parameters is ";

  18. Print_r ($args);

  19. Echo '
    ';

  20. }

  21. }//the end of class

  22. Instantiate an Object

  23. $p =new person ();

  24. Calling methods that exist in the person class

  25. $p->walk (5,39);

  26. Calling a method that does not exist in the person class

  27. $p->work ();//If the Magic method __call is not used, the program has an error, error message fatal Error:call to undefined method person::work () in d:/phpnow/htdocs/ holiday/magic__call.php on line 29

  28. If the Magic method __call is used, __call is called automatically when an object calls a function that does not exist in the class, and is used to perform certain operations.

  29. ?>

Copy Code

5.__clone ()

This function is called automatically when an object is cloned, and it acts to initialize the cloned copy.

  1. Initialize a class

  2. Class computer{

  3. //

  4. Public $cpu = ' Intel ';

  5. Private $monitor;

  6. Private $keyboard;

  7. Set Magic Method __clone () function

  8. function __clone () {//If you do not set this magic method then the clone is exactly the same, the __clone function and the constructor of the class are similar to do some initialization operations

  9. $this->cpu= ' AMD ';//$this refers to the newly created clone object

  10. }

  11. Set a destructor to compare the difference between = and clone

  12. function __destruct () {

  13. Echo ' *************************************
    ';

  14. }

  15. }//the end of the class

  16. Instantiate an Object

  17. $c 1=new computer ();

  18. Use the referenced method to add a reference, because the object's reference is stored in memory in the stack memory, and if you press the pointer, then it is in memory that there is a pointer to the object that $c1 points to

  19. We can tell by the destructor of the bar that this is not a copy of the object.

  20. $c 2= $c 1;

  21. Cloning an object using the Clone method

  22. $c 3=clone $c 1;

  23. Information comparison of the outputs

  24. echo $c 1->cpu, '
    ';

  25. echo $c 3->cpu, '
    ';

  26. ?>

Copy Code

6.__get () When the member property called by the object is not declared or the level is private or protected, we can perform some of our own operations in this function.

    1. Class person{

    2. Private $name;

    3. Public $nation;

    4. function __get ($para) {//must have one parameter

    5. echo $para. ' Not present ';

    6. }

    7. }

    8. $p =new person ();

    9. $name = $p->name;//call the __get function

    10. $nation = $p->nation;//never Call the __get function

    11. ?>

Copy Code

7.__set () Call this function when an assignment is made to an undeclared or level of private or protected, and we can do some of our own work in this function

    1. Class pc{

    2. Private $key;

    3. function __set ($key, $value) {//must have two parameters one is a member property and the other is a value

    4. echo ' pair '. $key. ' Assigned to '. $value. ' Failure ';

    5. }

    6. }

    7. $p =new Pc ();

    8. $p->key=123;

    9. ?>

Copy Code

1 2 Next last page

  • 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.