目次
PHPで最初から用意されている(定義済み)の主な定数
最初から用意されている主な関数
| __FILE__ | 実行中のファイル名(絶対パス) |
| __DIR__ | 実行中のファイルが存在するフォルダ |
| __LINE__ | 実行中の行番号 |
| __FUNCTION__ | 実行中の関数名 |
| __CLASS__ | 実行中のクラス名(所属する名前空間を含む) |
| __METHOD__ | 実行中のメソッド名 |
| __TRAIT__ | 実行中のトレイト名(所属する名前空間を含む) |
| __NAMESPACE__ | 現在の名前空間 |
| DIRECTORY_SEPARATOR | フォルダの区切り文字(windowsでは「¥」、macでは「/」) |
| PATH_SEPARATOR | パスの区切り文字(windowsでは「;」、macでは「:」) |
| PHP_VERSION | 使用しているPHPのバージョン |
※アンダーラインは2つです。
実行結果
<?php
var_dump(__FILE__);
//実行結果
//string(34) "/Applications/MAMP/htdocs/test.php"
?><?php
var_dump(__DIR__);
//実行結果
//string(25) "/Applications/MAMP/htdocs"
?><?php
var_dump(__LINE__);
//実行結果
//int(2)
?><?php
function test(){
var_dump(__FUNCTION__);
}
test();
//実行結果
//string(4) "test"
?><?php
class test{
function test(){
var_dump(__CLASS__);
}
}
$test = new test;
$test->test();
//実行結果
//string(4) "test"
?><?php
class test{
function test(){
var_dump(__METHOD__);
}
}
$test = new test;
$test->test();
//実行結果
//string(10) "test::test"
?><?php
trait test{
function test(){
var_dump(__TRAIT__);
}
}
class hoge{
use test;
}
$test = new hoge;
$test->test();
//実行結果
//string(4) "test"
?><?php
namespace test{
function test(){
var_dump(__NAMESPACE__);
}
test();
}
//実行結果
//string(4) "test"
?><?php
var_dump(DIRECTORY_SEPARATOR);
//実行結果 ※mac
//string(1) "/"
?><?php
var_dump(PATH_SEPARATOR);
//実行結果 ※mac
//string(1) ":"
?><?php
var_dump(PHP_VERSION);
//実行結果
//string(5) "8.0.3"
?>