Introduction to magic functions and magic constants in PHP _ PHP Tutorial

Source: Internet
Author: User
PHP magic functions and magic constants. Magic function _ construct () is called when an object is instantiated. When _ construct and a function with the class name coexist, __construct is called, and the other is not called. _ Destr magic function
  1. _ Construct ()
  2. An object is called when it is instantiated. When _ construct and a function with the class name coexist, __construct will be called, and the other will not be called.

  3. _ Destruct ()
  4. It is called when an object or object operation is terminated.

  5. _ Call ()
  6. An object calls a method. if a method exists, it is called directly. if the method does not exist, the _ call function is called.

  7. _ Get ()
  8. When reading an object property, if the property exists, the property value is directly returned. if the property does not exist, the _ get function is called.

  9. _ Set ()
  10. If an object property exists, the value is assigned directly. if the property does not exist, the _ set function is called.

  11. _ ToString ()
  12. It is called to print an object. Such as echo $ obj; or print $ obj;

  13. _ Clone ()
  14. Called when cloning an object. For example: $ t = new Test (); $ t1 = clone $ t;

  15. _ Sleep ()
  16. Serialize is called before. If the object is large and you want to delete something and serialize it, consider this function.

  17. _ Wakeup ()
  18. Unserialize is called to initialize objects.

  19. _ Isset ()
  20. It is called to check whether an object property exists. For example, isset ($ c-> name ).

  21. _ Unset ()
  22. Unset an object property is called. For example, unset ($ c-> name ).

  23. _ Set_state ()
  24. Called when var_export is called. Use the return value of _ set_state as the return value of var_export.

  25. _ Autoload ()
  26. When instantiating an object, if the corresponding class does not exist, this method is called.

Magic constant
  1. _ LINE __
  2. Returns the current row number in the file.

  3. _ FILE __
  4. Returns the complete file path and file name. If it is used in a include file, the include file name is returned. Starting from PHP 4.0.2, __file _ always contains an absolute path, while earlier versions sometimes contain a relative path.

  5. _ FUNCTION __
  6. Returns the function name (new in PHP 4.3.0 ). Starting from PHP 5, this constant returns the name (case sensitive) when the function is defined ). In PHP 4, the value is always lowercase letters.

  7. _ CLASS __
  8. The name of the returned class (new in PHP 4.3.0 ). Starting from PHP 5, this constant returns the name (case sensitive) when the class is defined ). In PHP 4, the value is always lowercase letters.

  9. _ METHOD __
  10. Method name of the returned class (new PHP 5.0.0 ). Returns the name (case sensitive) when the method is defined ).

First knowledge magic methods

Since the release of Php5.0, many object-oriented features have been provided for us, especially a lot of easy-to-use magic methods. these magic methods allow us to simplify our coding, better design our system. Today, let's get to know the magic methods provided by php5.0.

_ 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: name does not exist.

_ 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 ''. $ key. "Value ". $ value ;}}$ t = new Test (); $ t-> name = "aninggo ";

The output is aninggo, the value of name.

_ 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 );

The program will output: the getName method you want to call does not exist.

The parameter is:

Array([0] => aning[1] => go)

_ 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

_ Clone () is called when an object is cloned.

Class Test {public function _ clone () {echo "I have been copied! ";}}$ T = new Test (); $ t1 = clone $ t;

Program output: I have been copied

Struct _ construct () is called when an object is instantiated. When _ construct and a function with the function name of the class coexist, __construct is called, and the other is not called. _ Destr...

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.