简体   繁体   English

php-扩展给定类的所有类的列表

[英]php - list of all classes that extends a given class

I am building a little cms and I would like to know which is the best way to preceed. 我正在建立一点cms,我想知道哪种是最好的方法。

Suppose I have classes myClassA , myClassB , myClassC , ... that extend a given class myClass . 假设我有类myClassAmyClassBmyClassC ,它们扩展了给定的类myClass

I need a function to list all the classes MyClass* that extend MyClass . 我需要一个函数来列出扩展MyClass所有类MyClass* Is there a simple and safe way to do this just with PHP, or should I mantain the list somewhere else (maybe a table in the database)? 是否有一种简单安全的方法可以仅使用PHP来执行此操作,还是应该将列表保留在其他位置(可能是数据库中的表)?

I hope the question is clear enough... 我希望这个问题很清楚...

I would use scandir(C:// .... /[directory with files in]); 我会用scandir(C:// .... / [包含文件的目录]); to get an array containing all files and folders in that selected directory. 获取包含所选目录中所有文件和文件夹的数组。

I would then remove '.' 然后,我将删除“。” and '..' as these are for navigation of directories. 和“ ..”,因为它们用于目录导航。 Then in a foreach() loop use if(! is_dir($single_item)) to get all files that aren't directories. 然后在foreach()循环中,使用if(!is_dir($ single_item))来获取不是目录的所有文件。 After this you have a list of files and directories. 之后,您将获得文件和目录的列表。 I would then remove the directory navigation '.' 然后,我将删除目录导航“。” and '..' from the array. 和数组中的“ ..”。

Then as before I would read the contents of the file using file_get_contents(), then split the words using explode() exploding by a [space]. 然后像以前一样,我将使用file_get_contents()读取文件的内容,然后使用explode()将单词拆分成[space]。 I would then use a regex expression '~MyClass[A-Za-z0-9]~' (or other applicable expression) using preg_match() and store all matches in an array. 然后,我将使用preg_match()使用正则表达式'〜MyClass [A-Za-z0-9]〜'(或其他适用的表达式),并将所有匹配项存储在数组中。 I would finally filter these by using array_filter() to get a unique list you can use however you like 我最终将通过使用array_filter()过滤它们以获取可以使用的唯一列表,但是您喜欢

//directory to scan
$dir = "C:\ ...\[directory you want]";

//scan root directory for files
$root = scandir($dir);

//delete directory listings from array (directory navigation)
$disallowed_values = array(".", "..");
foreach($disallowed_values as $disallowed)
{
    if(($key = array_search($disallowed, $root)) !== false)
    {
        unset($root[$key]);
    }
}

//if array is not empty (no files / folders found)
if(! empty($root))
{
    //empty array for items you want found.
    $class_array = array();

    //for each directory
    foreach($root as $item)
    {
        if(! is_dir("$dir" . DIRECTORY_SEPARATOR . "$item"))
        {
            //get file contents
            $file_content = file_get_contents("$dir" . DIRECTORY_SEPARATOR . "$item");

            //pattern to search for
            $pattern = "~MyClass[A-Za-z0-9]*~";

            //create array with results for single file
            preg_match_all($pattern, $file_content, $result);

            //use $result to populate class_array(); use print_r($result); to check what it is outputting (based on your file's structures)
        }
    }
}

//get unique items from array_filter - remove duplicates
$class_array = array_filter($class_array);

//use array of items however you like

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM