Hằng số ma thuật PHP
Các hằng số được xác định trước của PHP
PHP có chín hằng số được xác định trước có giá trị thay đổi tùy thuộc vào nơi chúng được sử dụng và do đó chúng được gọi là "hằng số ma thuật".
Các hằng số ma thuật này được viết bằng dấu gạch dưới kép ở đầu và cuối, ngoại trừ hằng số ClassName::class.
Hằng số ma thuật
Dưới đây là các hằng số kỳ diệu, kèm theo mô tả và ví dụ:
Constant | Description | |
---|---|---|
__CLASS__ | If used inside a class, the class name is returned. | Try it » |
__DIR__ | The directory of the file. | Try it » |
__FILE__ | The file name including the full path. | Try it » |
__FUNCTION__ | If inside a function, the function name is returned. | Try it » |
__LINE__ | The current line number. | Try it » |
__METHOD__ | If used inside a function that belongs to a class, both class and function name is returned. | Try it » |
__NAMESPACE__ | If used inside a namespace, the name of the namespace is returned. | Try it » |
__TRAIT__ | If used inside a trait, the trait name is returned. | Try it » |
ClassName::class | Returns the name of the specified class and the name of the namespace, if any. | Try it » |
Ghi chú:
Các hằng số ma thuật không phân biệt chữ hoa chữ thường, nghĩa là __LINE__
trả về giống như __line__
.