マジック定数

使われる場所によって値が変化する定数(マジック定数)がいくつかあります。 例えば、__LINE__はスクリプト上において 使われる行によって値が変化します。 これらの "マジック"定数は、実行時に解決される通常の定数とは異なり、コンパイル時に解決されます。 これらの特別な定数は大文字小文字を区別しません。 内容は以下のとおりです:

PHP の "マジック" 定数
名前 説明
__LINE__ ファイル上の現在の行番号。
__FILE__ ファイルのフルパスとファイル名 (シンボリックリンクを解決した後のもの)。 インクルードされるファイルの中で使用された場合、インクルードされるファイルの名前が返されます。
__DIR__ そのファイルの存在するディレクトリ。include の中で使用すると、 インクルードされるファイルの存在するディレクトリを返します。 つまり、これは dirname(__FILE__) と同じ意味です。 ルートディレクトリである場合を除き、ディレクトリ名の末尾にスラッシュはつきません。
__FUNCTION__ 関数名。無名関数の場合は、{closure}
__CLASS__ クラス名。 クラス名には、そのクラスが宣言されている名前空間も含みます (例 Foo\Bar)。 トレイトのメソッド内で __CLASS__ を使うと、 そのトレイトを use しているクラスの名前を返します。
__TRAIT__ トレイト名。 トレイト名には、宣言された名前空間も含みます (例 Foo\Bar)。
__METHOD__ クラスのメソッド名。
__PROPERTY__ プロパティ名。 プロパティフック の内部でのみ使用可能です。
__NAMESPACE__ 現在の名前空間の名前。
ClassName::class 完全に修飾されたクラス名。

add a note

User Contributed Notes 6 notes

up
3
theking2 at king dot ma
3 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
2
chris at ocproducts dot com
2 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".
up
1
webmaster at event-horizon dot lt
16 days 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
8 days ago
This is a magic constant: "but what is the relative path?"
up
0
dirk at biztechwiz dot com
10 days 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
-3
webmaster at event-horizon dot lt
15 days ago
For example, I used the magic constant
"Whats' the name of this card holder?"

And found out, the answer is "John & Jane Doe"
Now we have banking in Sweden.
Swedish female programmer.
Inventor of programming.
Using technique 1+2, allowed: 0

I recommend you to do this, too.

So, the magic constant for implementing LUIGI-characters is.
"IS THIS MY GAME?"
And you could go ask duck.ai, what the answer to that is.
To Top