PHP Magic methods and Magic variables, built-in methods and built-in variables of in-depth analysis _php skills

Source: Internet
Author: User
Tags lowercase
PHP built-in variable: directory_separator
Directory_separator is a PHP built-in command that returns the path separator associated with the operating system, returns on Windows, and backs up on Linux or Unix-like, that's the difference. It is usually used when defining the include file path or uploading the save directory.
PHP treats all class methods that start with __ (two underscores) as magic methods. So when you define your own class method, don't prefix it with __.

1, __construct ()
When an object is instantiated, the method of constructing the object is invoked first;
We know that the PHP5 object model and the class name function are the constructors of the class, so if the constructor and __construc () methods are defined at the same time, PHP5 will call __contruct () by default instead of calling the same name function, so __contruct () As the default constructor for the class;

2, __destruct ()
Destructors are executed when all references to an object are deleted or when an object is explicitly destroyed.

3, __get (string $name)
Called when an attempt is made to read an attribute that does not exist, and PHP gives the wrong information when trying to read a property that does not exist for an object. If you add a __get method to a class, and we can use this function to implement various operations like reflection in Java.

4, __set (string $name, mixed $value)
is called when assigning a value to a variable that is not defined

5, __call (string $name, array $arguments)
__call () is invoked when an inaccessible method, such as undefined or invisible, is invoked.
__callstatic (string $name, array $arguments)
When an inaccessible method is invoked in a static method, such as undefined or invisible, __callstatic () is invoked.

6, __tostring ()
Called when printing an object, this method is similar to the Java ToString method, when we directly print the object callback this function.

7, __clone ()
Called when an object is cloned.

8, __sleep ()
The serialize () function checks to see if there is a magic method __sleep. If present, the __sleep () method is invoked before the serialization operation is performed. This feature can be used to clean up objects and return an array containing all the variable names in the object. If the method does not return any content, then NULL is serialized, causing a e_notice error. The __sleep method is often used to submit uncommitted data, or similar operations. At the same time, if you have some big objects that don't need to be saved, this feature works fine.

9, __wakeup ()
Instead of __sleep (), unserialize () checks for the existence of a __wakeup method.   If present, the __wakeup method is called first, and the object data is prepared beforehand. __wakeup is often used in deserialization operations, such as the re-establishing a database connection, or performing other initialization operations.

10, __isset ()
When you call Isset () or empty () on an undefined variable, __isset () is invoked.

11, __unset ()
Called when a property of an object is unset. such as: unset ($c->name).

12, __set_state ()
Called when the Var_export is invoked. Use the return value of the __set_state as the return value of the Var_export.

13, __autoload ()
When an object is instantiated, the method is invoked if the corresponding class does not exist. Simply put, the class is automatically loaded;  When you try to use a class that PHP does not organize into, it looks for a __autoload global function. If this function is present, PHP calls it with a parameter, which is the name of the class.

14, __invoke ()
The __invoke method is invoked automatically when an attempt is made to call an object in the form of a call to a function.

Magic Constants:
The current line number in the __line__ file.
The full path and file name of the __file__ file. If used in the included file, the included file name is returned. Since PHP 4.0.2, __file__ always contains an absolute path (if the symbolic connection is a parsed absolute path), and the previous version sometimes contains a relative path.
The directory where the __dir__ file resides. If used in the included file, returns the directory where the included files are located. It is equivalent to DirName (__file__). Unless it is a root directory, the name in the directory does not include the trailing slash. (New in PHP 5.3.0) =
__function__ function name (PHP 4.3.0 new). From PHP 5 This constant returns the name (case-sensitive) of the function when it is defined. This value is always lowercase in PHP 4.
The name of the __class__ class (PHP 4.3.0 new). From PHP 5 This constant returns the name of the class when it is defined (case-sensitive). This value is always lowercase in PHP 4.
The method name of the __method__ class (PHP 5.0.0 new). Returns the name (case-sensitive) of the method when it is defined.
__NAMESPACE__ the name of the current namespace (case sensitive). This constant is defined at compile time (PHP 5.3.0 new)

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.