Home  >  Article  >  Backend Development  >  what does php magic mean

what does php magic mean

藏色散人
藏色散人Original
2021-03-11 10:18:571940browse

Magic in php has two meanings, namely: 1. Refers to magic methods. PHP reserves all class methods starting with two underscores as magic methods; 2. Refers to magic constants, which PHP operates on. A large number of predefined constants are provided for any script.

what does php magic mean

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

Magic method

PHP reserves all class methods starting with __ (two underscores) as magic methods. Therefore, when defining class methods, except for the above magic methods, it is recommended not to prefix them with __.

__construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __serialize(), Methods such as __unserialize(), __toString(), __invoke(), __set_state(), __clone() and __debugInfo() are called magic methods in PHP. You cannot use these method names when naming your own class methods unless you want to use their magic functionality.

Note: All magic methods must be declared as public

Magic constants

PHP provides a large number of predefined constants to any script it runs . However, many constants are defined by different extension libraries and will only appear when these extension libraries are loaded, either dynamically loaded or included at compile time.

There are eight magic constants whose values ​​change depending on their position in the code. For example, the value of __LINE__ depends on the line it is in the script. These special constants are not case-sensitive, as follows:

Several PHP "magic constants"

__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.

__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.

__FUNCTION__: The name of the current function. An anonymous function is {closure}.

__CLASS__: The name of the current class. The class name includes the scope in which it is declared (e.g. Foo\Bar). Note that since PHP 5.4 __CLASS__ also works for traits. When used within a trait method, __CLASS__ is the name of the class that calls the trait method.

__TRAIT__: The name of Trait. The trait name includes the scope in which it is declared (e.g. Foo\Bar).

__METHOD__: The method name of the class.

__NAMESPACE__: The name of the current namespace.

ClassName::class: Complete class name, see ::class.

See get_class() get_object_vars(), file_exists() and function_exists().

[Recommended learning: "PHP Video Tutorial"]

The above is the detailed content of what does php magic mean. 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