Updated PHP Windows builds for Brotli security fix

マジック定数

マジック定数は、ソースコード上の位置に関する情報に置き換えられるキーワードです。 置き換えられるのは、記述されているファイルと行、およびその箇所を囲む関数、クラス、トレイト、名前空間、プロパティフックの情報です。 「定数」という名前が付いていますが、実際の定数ではありません。 defined() と constant() はマジック定数を認識せず、 define() で同じ名前の定数を作成しても、マジック定数には影響しません。 マジック定数は大文字小文字を区別しません。 クラスの外での __CLASS__ のように、該当する文脈の外で使用すると、 空文字列に置き換えられます。

PHP の "マジック" 定数
名前 説明
__LINE__ 記述されている行の番号。
__FILE__ 記述されているファイルのフルパスとファイル名。 シンボリックリンクは解決されます。
__DIR__ そのファイルの存在するディレクトリ。 これは dirname(__FILE__) と同じ意味です。 ルートディレクトリである場合を除き、ディレクトリ名の末尾にスラッシュはつきません。
__FUNCTION__ 記述されている関数の名前。 関数名には、その関数が宣言されている名前空間も含まれます (例 Foo\bar)。 メソッドの中では、メソッド名だけになります。 無名関数の場合は、クロージャが定義された場所を含む、エンジンが生成した名前になります (PHP 8.4.0 以降)。それより前のバージョンでは {closure} です。
__CLASS__ 記述されているクラスの名前。 クラス名には、そのクラスが宣言されている名前空間も含まれます (例 Foo\Bar)。 トレイトの中では、そのトレイトを use しているクラスの名前になります。 無名クラス の場合は、エンジンが生成した名前になります。 メソッドの呼び出し先となったクラスの名前を取得するには、 static::class を使ってください。
__TRAIT__ トレイト名。 トレイト名には、宣言された名前空間も含まれます (例 Foo\Bar)。
__METHOD__ 記述されているメソッドのクラス名とメソッド名 (例 Foo\Bar::baz)。 トレイトの中では、クラス名ではなくトレイト名が使われます。 メソッドの外では、__FUNCTION__ と同じです。
__PROPERTY__ プロパティフック の内部で使うと、そのプロパティの名前になります。 PHP 8.4.0 以降で利用可能です。
__NAMESPACE__ 現在の名前空間の名前。

例1 マジック定数の使用例

<?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
+add a note

User Contributed Notes 5 notes

up
4
theking2 at king dot ma
4 years ago
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.
up
1
webmaster at event-horizon dot lt
7 months ago
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?"
up
0
Athralon dot media at gmail dot com
6 months ago
This is a magic constant: "but what is the relative path?"
up
0
dirk at biztechwiz dot com
6 months ago
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
up
-2
chris at ocproducts dot com
3 years ago
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".
To Top