Home >Backend Development >PHP Tutorial >In-depth analysis of PHP magic methods and magic variables, built-in methods and built-in variables_PHP Tutorial
PHP built-in variables: DIRECTORY_SEPARATOR
DIRECTORY_SEPARATOR is a PHP built-in command that returns the path separator related to the operating system. It returns / on Windows, and on Linux or Unix-like Go back on /, this is the difference. It is usually used when defining the path to include files or uploading and saving directories.
PHP treats all class methods starting with __ (two underscores) as magic methods. So when you define your own class methods, do not prefix them with __.
1, __construct()
When instantiating an object, the constructor method of this object will be called first;
We know that the php5 object model and the class name are the same The function is the constructor of the class, so if the constructor and __construc() method are defined at the same time, php5 will call __contruct() by default instead of calling a function of the same name, so __contruct() serves as 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)
is called when trying to read a property that does not exist; if trying to read a property that does not exist in an object , php will give wrong information. If we add the __get method to the class, we can use this function to implement various operations similar to reflection in java.
4 , __set(string $name, mixed $value)
will be called when assigning a value to an undefined variable
5 , __call( string $name, array $arguments)
When calling an inaccessible method (such as undefined, or invisible), __call() will be called.
__callStatic( string $name, array $arguments )
When an inaccessible method (such as undefined, or invisible) is called in a static method, __callStatic() will be called.
6, __toString()
is called when printing an object. This method is similar to java's toString method. This function is called back when we print the object directly.
7, __clone()
Called when the object is cloned.
8, __sleep()
serialize() function will check whether there is a magic method __sleep. If it exists, the __sleep() method will be called first, and then serialization is performed. operate. This function can be used to clean an object and return an array containing the names of all variables in the object. If the method returns nothing, NULL is serialized, resulting in an E_NOTICE error. The __sleep method is often used to submit uncommitted data, or similar operations. At the same time, this function is very useful if you have some large objects that do not need to be saved.
9, __wakeup()
Contrary to __sleep(), unserialize() checks whether there is a __wakeup method. If it exists, the __wakeup method will be called first 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 on an undefined variable, __isset() will be called.
11, __unset()
is called when unset the properties of an object. For example: unset($c->name).
12, __set_state()
is 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. Simply put, it is automatic loading of classes; when you try to use a class that PHP has not organized, it will look for a global function __autoload. If this function exists, PHP will call it with a parameter, which is the name of the class. .
14, __invoke()
When trying to call an object by calling a function, the __invoke method will be automatically called.
Magic constant:
__LINE__ The current line number in the file.
__FILE__ The full path and file name of the file. If used within an included file, returns the name of the included file. As of PHP 4.0.2, __FILE__ always contains an absolute path (or the resolved absolute path in the case of a symbolic link), whereas previous versions sometimes contained a relative path.
__DIR__ The directory where the file is located. If used within an included file, returns the directory where the included file is located. It is equivalent to dirname(__FILE__) . Directory names do not include the trailing slash unless they are the root directory. (New in PHP 5.3.0) =
__FUNCTION__ Function name (New in PHP 4.3.0). Since PHP 5 this constant returns the name of the function when it was defined (case sensitive). In PHP 4 this value is always lowercase.
__CLASS__ The name of the class (new in PHP 4.3.0). Since PHP 5 this constant returns the name of the class when it was defined (case sensitive). In PHP 4 this value is always lowercase.
__METHOD__ The method name of the class (newly added in PHP 5.0.0). Returns the name of the method as it was defined (case-sensitive).
__NAMESPACE__ The name of the current namespace (case sensitive). This constant is defined at compile time (new in PHP 5.3.0)