Include and require files within included files in PHP
Today, I noticed this nice piece of code within the SimpleTest autorun.php file.
require_once dirname(__FILE__) . '/unit_tester.php';
Finding the folder of the current file, and not the executing file, is extremely useful when including/requiring files within files that are included by another page.
When using dirname(__FILE__), the require operation works relative autorun.php, even if the file is included by another page. If this would not have been used, the page would have tried to look for the unit_tester.php file relative the including file, which would not have worked.
I have previously solved this kind of folder issues with an “application root” path, but this is also a very nice approach.
Thank you SimpleTest.