수업이 PHP에 포함 된 파일 이름을 얻는 방법
문제
나는 질문이 다소 이해하기 어렵다는 것을 이해하고, 더 잘 묻는 방법을 몰랐 으므로이 코드 예제를 사용하여 더 명확하게 할 것입니다.
다음 파일이있는 경우 :
test.php :
<?php
include('include.php');
echo myClass::myStaticFunction();
?>
포함 .php
<?php
__autoload($classname){
include_once("class/".$classname.".php"); //normally checking of included file would happen
}
?>
클래스/myclass.php
<?php
class myClass{
public static function myStaticFunction(){
//I want this to return test.php, or whatever the filename is of the file that is using this class
return SOMETHING;
}
?>
매직 파일 상수는 올바른 파일이 아니며 경로를 반환/myclass.php를 반환합니다.
해결책
"test.php"를 받아야하는 경우 $_SERVER['SCRIPT_NAME']
다른 팁
나는 다음을 사용했다.
$file = basename(strtolower($_SERVER['SCRIPT_NAME']));
나는 사용 중입니다
$arr = @debug_backtrace(false);
if (isset($arr))
foreach ($arr as $data)
{
if (isset($data['file']))
echo $data['file'];
// change it to needed depth
}
이렇게하면 파일이 포함 된 파일을 수정할 필요가 없습니다. Debug_backtrace에는 속도가 약간 높아질 수 있습니다.
제휴하지 않습니다 StackOverflow