[英]Is it possible to get the list of the class alias in php as array?
例子
namespace Foo;
use Test\One;
use Test\Two;
use Test\Three;
class Sample
{}
如何將別名(USE)作為數組獲取?
我希望得到的例子
$test = [測試\\一,測試\\二,測試\\樹];
有人在不掃描文件的情況下有什么建議嗎?
或者是否有一個 PHP 函數可以將列表別名作為數組返回?
任何幫助將不勝感激。
假設我有以下類,並且文件位於並命名為以下文件名和位置 src/Foo.php
namespace Foo;
use Test\One;
use Test\Two;
use Test\Three;
class Sample
{}
現在我可以掃描這個文件
使用此功能,我可以掃描該類並獲得預期的結果。
<?php
use \SplFileObject;
class Scanner
{
public static function getUseAliases()
{
$className = new SplFileObject("src/Foo.php");
$use = [];
while (!$className->eof())
{
$alias = explode("use ", $className->fgets());
if(!empty($alias[1]))
{
$use[] = trim($alias[1]);
}
}
$className = null; //Unset the file to prevent memory leaks
print_r($use);//will print my expected output [Test\One, Test\Two, Test\Three]
}
}
我認為應該有更好的方法來獲得相同的結果,這就是我發布當前解決方案的原因。 請讓我知道你的想法。
你可以嘗試使用這個類:
https://gist.github.com/Zeronights/7b7d90fcf8d4daf9db0c
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.