I am building a little cms and I would like to know which is the best way to preceed.
Suppose I have classes myClassA
, myClassB
, myClassC
, ... that extend a given class myClass
.
I need a function to list all the classes MyClass*
that extend 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)?
I hope the question is clear enough...
I would use scandir(C:// .... /[directory with files in]); 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. 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]. 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. I would finally filter these by using array_filter() to get a unique list you can use however you like
//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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.