Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

adding a directory of classes #3

Artistan opened this Issue May 20, 2011 · 1 comment


None yet
3 participants

I added this to code, I have and autoloader included in my framework so it works well

// this is to use with magic autoinclude.
public function get_dir_classes($lib_dir,$subdir,$seperator="_",$recurse=true,$follow_symlic=false){

    $full_dir = $lib_dir.DIRECTORY_SEPARATOR.$subdir;

    //using the opendir function
    $dir_handle = @opendir($full_dir) or die("Unable to open $full_dir");
    //running the while loop
    while ($file = readdir($dir_handle)){
        if($file!="." && $file!=".."){
                //echo "<a href='$file'>$file</a><br/>";
                $classes[] = str_replace(DIRECTORY_SEPARATOR,$seperator,$subdir).$seperator.str_replace('.php','',$file);
            } elseif($recurse && is_dir($full_dir.DIRECTORY_SEPARATOR.$file) && ($follow_symlic || !is_link($full_dir.DIRECTORY_SEPARATOR.$file)) && $file<>'.svn') {
                $more_files = $this->get_dir_classes($lib_dir,$subdir.DIRECTORY_SEPARATOR.$file,$seperator,$recurse,$follow_symlic);
                $classes = array_merge($classes,$more_files);
    //closing the directory
    return $classes;

public function add_dir($libdir,$subdir='rest',$basePath = '',$seperator="_",$recurse=true,$follow_symlic=false){
    $list = $this->get_dir_classes($libdir,$subdir,$seperator,$recurse,$follow_symlic);
    foreach($list as $class){
        // function get_dir_classes adds the prefix and seperator to class name for autoloading

jk commented Aug 30, 2011

This should be better implemented as an autoloader function, IMO.

@jacwright jacwright closed this Feb 17, 2014

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