This is a magic constant: "but what is the relative path?"
Magic constants are keywords that are replaced with information about
their own location in the source code: their file and line, and the
function, class, trait, namespace, or property hook enclosing them.
Despite their name, they are not actual constants:
defined() and constant() do not
recognize them, and a constant of the same name created with
define() does not affect them. They are case-insensitive.
When used outside of the relevant context (e.g. __CLASS__
outside of a class), they are replaced with an empty string.
| Назва | Опис |
|---|---|
__LINE__ |
The line number on which it is written. |
__FILE__ |
The full path and filename of the file in which it is written, with symlinks resolved. |
__DIR__ |
The directory of the file, equivalent to
dirname(__FILE__). It has no trailing slash
unless it is the root directory.
|
__FUNCTION__ |
The name of the function in which it is written, including its namespace
(e.g. Foo\bar).
Inside a method, only the method name.
For anonymous functions, a name generated by the engine that includes
where the closure was defined (as of PHP 8.4.0; previously
{closure}).
|
__CLASS__ |
The name of the class in which it is written, including its namespace
(e.g. Foo\Bar).
Inside a trait, the name of the class using the trait.
For anonymous classes,
a name generated by the engine.
To get the name of the class a method was called on, use
static::class.
|
__TRAIT__ |
The trait name, including its namespace (e.g. Foo\Bar).
|
__METHOD__ |
The class and method name of the method in which it is written
(e.g. Foo\Bar::baz).
Inside a trait, the trait name is used instead of the class name.
Outside of a method, the same as __FUNCTION__.
|
__PROPERTY__ |
The name of the property, when used inside a property hook. Available as of PHP 8.4.0. |
__NAMESPACE__ |
The name of the current namespace. |
Приклад #1 Using magic constants
<?php
namespace App;
function helper()
{
echo __FUNCTION__, "\n";
echo __METHOD__, "\n";
}
trait Greets
{
public function greet()
{
echo __CLASS__, "\n";
echo __TRAIT__, "\n";
echo __METHOD__, "\n";
echo __FUNCTION__, "\n";
}
}
class Greeter
{
use Greets;
}
helper();
(new Greeter)->greet();
echo __NAMESPACE__, "\n";Поданий вище приклад виведе:
App\helper App\helper App\Greeter App\Greets App\Greets::greet greet App
This is a magic constant: "but what is the relative path?"If PHP is run inside a web server request there is an important difference between the __DIR__ constant and $_SERVER['DOCUMENT_ROOT'].
Where __DIR__ of a PHP script contained within a sub-folder will include the complete server path $_SERVER['DOCUMENT_ROOT'] will contain a server path up to the _root_ of the application. This can be helpful when for instance an auto-loader is defined in an include file sitting inside a sub-folder and where the classes are located in another folder at the root of the application.I have got this script:
<?=
$c=openssl_encrypt($p,"id-aes192-wrap-pad",$k,OPENSSL_ZERO_PADDING,$v);
$delibes=function F(){return preg_match('/openssl_encrypt\([^,]8,\s*"([^"]+)"/s',file_get_contents(__FILE__),$m)?$m[1]:null;}
// Usage:
// echo $GLOBALS['delibes']
// Note-to-self: The editables, should not be changed
I have no idea how to change __FILE__, perhaps using __DIR__."but what is the relative path?"Note that __CLASS__ and __METHOD__ both reference the class the code is written in, not whatever the object class is. E.g. if you have an object of class B inheriting from class A, any usage of __CLASS__ in class A is going to give "A".Getting the magic constants from the right place is important. For example, creating a function to log location information requires explicit inclusion.
<?php
/***********
* magic_constants_include.php
*
* Magic constant test include file
*/
function showInclude($file=__FILE__, $lineno=__LINE__) {
echo "Parameter values: $file ($lineno)\n";
echo "Local constants: " . __FILE__ . "; " . __LINE__ . "\n";
}
?>
<?php
/************
* magic_constants_test.php
*/
require_once 'magic_constants_include.php';
echo "Function call with parameter defaults:\n";
showInclude();
echo "Function call with parameter explicit magic constants:\n";
showInclude(__FILE__, __LINE__);
?>
Results:
php -f magic_constants_test.php
Function call with parameter defaults:
Parameter values: /home/dirk/project/dirk/php/magic_constants_include.php (7)
Local constants: /home/dirk/project/dirk/php/magic_constants_include.php; 9
Function call with parameter explicit magic constants:
Parameter values: /home/dirk/project/dirk/php/magic_constants_test.php (12)
Local constants: /home/dirk/project/dirk/php/magic_constants_include.php; 9