In-depth analysis of php magic methods and magic variables, built-in methods and built-in Variables

Source: Internet
Author: User
Tags autoload

Php built-in variable: DIRECTORY_SEPARATOR
DIRECTORY_SEPARATOR is a php built-in command that returns path delimiters related to the operating system. It returns/on windows, and regrets/on linux or unix-like, it is usually used when defining the path containing files or uploading and storing directories.
PHP treats all class Methods Starting with _ (two underscores) as magic methods. Therefore, when you define your own class methods, do not use _ as the prefix.

1. _ construct ()
When an object is instantiated, the constructor of this object will be called first;
We know that the php5 object model and the function with the same class name are class construc functions. If the construc and _ construc () methods are defined at the same time, php5 calls _ contruct () by default instead of similar name functions. Therefore, _ contruct () is the default constructor of the class;

2. _ destruct ()
The Destructor is executed when all references to an object are deleted or when the object is explicitly destroyed.

3. _ get (string $ name)
It is called when you try 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 various operations similar to reflection in java.

4. _ set (string $ name, mixed $ value)
When an undefined variable is assigned a value, it is called.

5. _ call (string $ name, array $ arguments)
When an inaccessible method (such as undefined or invisible) is called, _ call () is called.
_ CallStatic (string $ name, array $ arguments)
When an inaccessible method (such as undefined or invisible) is called in a static method, _ callStatic () is called.

6. _ toString ()
This method is called when an object is printed. It is similar to the toString method of java. This function is called when the object is printed directly.

7. _ clone ()
Called when the object is cloned.

8. _ sleep ()
The serialize () function checks whether a magic method _ sleep exists. If so, the _ sleep () method is called first before the serialization operation is executed. This function can be used to clear an object and return an array containing all the variable names of the object. If this method does not return any content, NULL is serialized, resulting in an E_NOTICE error. The _ sleep method is often used to submit uncommitted data or perform similar operations. At the same time, if you have some large objects that do not need to be saved, this function is very useful.

9. _ wakeup ()
In contrast to _ sleep (), unserialize () checks whether a _ wakeup method exists. If yes, the _ wakeup method is called to prepare the object data in advance. _ Wakeup is often used in deserialization operations, such as re-establishing a database connection or performing other initialization operations.

10. _ isset ()
When isset () or empty () is called for undefined variables, _ isset () is called.

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

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

13. _ autoload ()
When instantiating an object, if the corresponding class does not exist, this method is called. In short, it is the automatic loading of classes. When you try to use a class that is not organized by PHP, it will look for a global function of _ autoload. if this function exists, PHP uses a parameter to call it. The parameter is the name of the class.

14. _ invoke ()
When you try to call an object by calling a function, the _ invoke method is automatically called.

Magic constant:
The current row number in the _ LINE _ file.
The complete path and FILE name of the _ FILE. If it is used in a file to be included, the file name to be included is returned. Since PHP 4.0.2, _ FILE _ always contains an absolute path (if it is a symbolic connection, it is an absolute path after resolution ), earlier versions sometimes contain a relative path.
The directory where the _ DIR _ file is located. If it is used in included files, the Directory of the included files is returned. It is equivalent to dirname (_ FILE __). Unless it is the root directory, the name of the directory does not include the slash at the end. (Added in PHP 5.3.0) =
_ 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.
_ CLASS name (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.
_ METHOD _ class METHOD name (New PHP 5.0.0 ). Returns the name (case sensitive) when the method is defined ).
_ NAMESPACE _ name of the current NAMESPACE (case sensitive ). This constant is defined during compilation (New in PHP 5.3.0)

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.