0

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.

1 Answer 1

2

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");
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Mike. All the class files are in different directories and for a reason. I tried using variable in glob function but it did not work and I guess glob function doesnt do any variable value substitution
@user2959149 did you include them in single quotes? A string in PHP is a string. It doesn't matter how it was created. If you give it the right string, it will work.
Thanks Mike. I tried the $fdir in glob and it does work. I was missing the forward slash.
When in doubt, echo, print_r() and var_dump() are your friends. Figure out what is actually contained in a variable by using them at strategic locations and your error should usually become apparent.
All I can say...still learning how to properly use/debug PHP...Thanks I 'll keep in mind

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.