Skip to content

Instantly share code, notes, and snippets.

@pwenzel
Created August 23, 2012 17:07
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save pwenzel/3438784 to your computer and use it in GitHub Desktop.
Save pwenzel/3438784 to your computer and use it in GitHub Desktop.
Recursively include all PHP files
<?php
/**
* Scan the api path, recursively including all PHP files
*
* @param string $dir
* @param int $depth (optional)
*/
protected function _require_all($dir, $depth=0) {
if ($depth > $this->max_scan_depth) {
return;
}
// require all php files
$scan = glob("$dir/*");
foreach ($scan as $path) {
if (preg_match('/\.php$/', $path)) {
require_once $path;
}
elseif (is_dir($path)) {
$this->_require_all($path, $depth+1);
}
}
}
@wellington1993
Copy link

Thanks...

@mrashad10
Copy link

Good work
if you changed line 15 to $scan = glob("$dir" . DIRECTORY_SEPARATOR . "*");
the code will be OS independent

Thank you

@robsoncompuff
Copy link

Thanks, good code!

@vroper123
Copy link

is this a save way to code?

@danbulant
Copy link

Remember that the included PHP files would be in _require_all function context, so variables won't be accessible outside. (This doesn't mean that functions and classes won't work)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment