Question

As mentioned in PHP Manual

pattern

The pattern. No tilde expansion or parameter substitution is done.

I tried to find files with path name in a variable and it didn’t work.

The loader file (main function in the program) needs to include few files which reside in the same directory of the present class file that the loader file is working on . So , I tried below work around in the loader file.

$cwd = getcwd();
$reflector = new ReflectionClass($class_name); //$class_name  come from $_GET
$fname = $reflector->getFileName();
$fdir = dirname($fname);
chdir ($fdir);
$include_file_name = glob("*.inc");
chdir ($cwd);

It works but just wants to know is it a good work around or is there anything else I can do.

Was it helpful?

Solution

If you have all of your classes inside one directory, you could just hard code it relative to the current script and that would eliminate the need for a reflection class. For example, if the current script is in /var/www/foo and the classes are in /var/www/classes, just do:

$include_file_name = glob(__DIR__ . '/../classes' . '*.inc');

However if the classes can come from multiple directories, using a reflector like that seems pretty appropriate to me, however you could also add $fdir into the glob() string instead of doing chdir() like this, but of course that's just micro-optimization:

$include_file_name = glob($fdir ."/*.inc");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top