Home  >  Article  >  Backend Development  >  What are the magic method functions in php5?

What are the magic method functions in php5?

青灯夜游
青灯夜游Original
2020-09-28 13:58:241778browse

The magic method functions in php5 include: "__sleep", "__wakeup", "__toString", "__construct", "__destruct", "__call", "__get", "__set", "__isset", " __unset" etc.

What are the magic method functions in php5?

Recommended: "PHP Video Tutorial"

Magic method function in php5

PHP5 contains many magic methods. Their most typical feature is that they all start with a double underscore '__'. These magic methods are written to complete certain specified functions.

  • __sleep, this function will be called before serialize() serializes the object.

  • __wakeup, this function will be called before unserialize() is deserialized.

  • __toString, this function is called when converting an object into a string.

  • __construct, constructor function, this function will be called when instantiating an object.

  • #__destruct, destructor, called when the object instance is released.

  • #__call, this function will be called when calling a method that is not in the class.

  • #__get, called when accessing an attribute value that is not in the class.

  • #__set, called when setting an attribute value that is not in the class.

  • #__isset, called when calling the external isset() function to check whether the class contains a certain attribute value.

  • __unset, called when a certain attribute value of the class is deleted.

  • __set_state, called when using var_export to export the properties and values ​​of the object.

  • __clone, called when copying a tired instance.

  • __autoload, which is automatically called when a class is used and the class has not been loaded into the page.

Magic constant:

  • __LINE__: Returns the current line number;

  • __FILE__: Returns the full path and file name of the file. If used in an included file, the included file name is returned. Starting from php4.0.2, __FILE__ always contains an absolute path, while in previous versions it sometimes included A relative path

  • __FUNCTION__: Returns the function name (newly added since php4.3.0). Since php5, this constant returns the name when the function is defined, which is case-sensitive. In php4, the value is always lowercase;

  • __CLASS__: Returns the name of the class, since php4.3.0 Newly added, since php5, this constant returns the name when the class is defined, case-sensitive, in php4 the value is always lowercase;

  • __METHOD__: Returns the method of the class name.

Related recommendations: php training

The above is the detailed content of What are the magic method functions in php5?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn