In depth analysis of php magic methods and magic variables built in methods and built in variables

  • 2020-06-03 06:12:40
  • OfStack

php has a built-in variable: DIRECTORY_SEPARATOR
DIRECTORY_SEPARATOR is an php built-in command that returns/on windows, whereas linux or class unix backout/is such a distinction that is commonly used when defining the path to include files or when you upload save directories.
PHP treats all class methods that begin with __ (2 underscores) as magic methods. So don't prefix your methods with __.

1, __construct ()
When an object is instantiated, its constructor is called first.
We know that the functions with the same php5 object model and class name are the class constructors, so if you define both the constructor and the method with each ___ 18en (), php5 calls succcontruct () by default and not by the class name, so ___ 21en () is the default constructor of the class;

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

3, __get (string $name)
Called when attempting to read a nonexistent property; php gives an error message if it attempts to read a property that does not exist in an object. If you add the method ___ in your class, and we can use this function to implement various operations similar to reflection in java.

, (string $name, mixed $value)
An undefined variable is called when it is assigned a value

, (string $name, array $arguments)
Each ___ () is called when an unreachable method (such as undefined or invisible) is called.
__callStatic( string $name, array $arguments )
Each ___ () is called when an unreachable method (such as undefined or invisible) is called in a static method.

6, __toString ()
Called when printing an object, this method is similar to java's toString method, which calls back to this function when we print the object directly.

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

8, __sleep ()
The serialize() function checks if there is a magic method with each ___ 71en. If there is, the method with each ___ 72en () is called before the serialization operation is performed. This function can be used to clean up 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 method with SUCCinct 76en is often used to submit uncommitted data, or something similar. At the same time, if you have 1 large object that you don't need to save, this function works well.

9, __wakeup ()
By contrast with ___ 81en (), unserialize() checks if there is a method of 1 ES83en. If it does, the method with SUCCwakeup is called first, pre-preparing the object data. ___ is often used in deserialization operations, such as reestablishing a database connection, or performing other initialization operations.

10, __isset ()
Each ___ 92en () is called when an undefined variable is called isset() or empty().

11, __unset ()
unset 1 object properties are called. Such as: unset ($c - > name).

12, __set_state ()
When var_export is called. Return the return value of each ___ 109EN_ES110en with var_export.

13, __autoload ()
When instantiating an object, the method is called if the corresponding class does not exist. Simply put, it is the automatic loading of classes; If you try to use a class to which PHP is not organized, it looks for the global function with 1 ___. If this function exists,PHP calls it with a single argument, the name of the class.

14, __invoke ()
The ___ 124en method is automatically called when you try to call an object as a calling function.

Magic constants:
Per ___ the current line number of ___.
The full path and filename of the file with succFILE__. Returns the included file name if used in the included file. Starting with PHP 4.0.2, ___ always contains 1 absolute path (or if it is a symbolic join, the resolved absolute path), whereas previous versions sometimes contain 1 relative path.
___ The directory with the document. If used in an included file, returns the directory where the included file is located. It's the equivalent of dirname(___, 136EN__). Unless it is the root directory, the directory name does not include the trailing slash. (new in PHP 5.3.0) =
___ The function name of ___ (PHP 4.3.0 plus). This constant since PHP 5 returns the name of the function when it was defined (case sensitive). In PHP 4 this value is always lowercase.
___, 144EN__ (PHP 4.3.0 plus). This constant since PHP 5 returns the name of the class when it was defined (case sensitive). In PHP 4 this value is always lowercase.
Method name of the class of SUCCes149EN__ (PHP 5.0.0 added). Returns the name of the method when it was defined (case sensitive).
The name of the current namespace of ___ (case sensitive). This constant is defined at compile time (new to PHP 5.3.0)

Related articles: