SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Good PHP Loader

  1. #1
    SitePoint Zealot kobra's Avatar
    Join Date
    Sep 2003
    Location
    Chicago
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Good PHP Loader

    I've been looking for a good PHP loader that will allow me to load my library classes and then use their object in other application classes. Can someone suggest something of that sort that I can use? Is the Registry method an option? And what's wrong with the following code:

    PHP Code:
    <?php



    class Loader {

      
    // framework libraries to be excluded
      
    var $__exclude_lib = array(
        
    "c"     => "controller",
        
    "load"  => "loader",
        
    "model" => "model",
        
    "obj"   => "object",
        
    "view"  => "view"
      
    );

      
    // framework libraries to be included
      
    var $__lib = array(
        
    "bmark" => "benchmark",
        
    "cache" => "cache",
        
    "conf"  => "config",
        
    "db"    => "database",
        
    "date"  => "date",
        
    "email" => "email",
        
    "err"   => "error",
        
    "file"  => "file",
        
    "dir"   => "folder",
        
    "i18n"  => "i18n",
        
    "log"   => "log",
        
    "sess"  => "session",
        
    "soap"  => "soap",
        
    "tmpl"  => "template",
        
    "valid" => "validate",
        
    "xml"   => "xml"
      
    );

      
    /**
       *  Loader constructor load all framework library and root files
       *
       *  @access  public
       *  @param   none
       *  @return  none
       */
      
    function Loader() {
      
        
    $load["lib"]  = $this->__get_lib_files();
        
    $load["root"] = $this->__get_root_files();

        foreach (
    $load as $type => $file) {
        
          for (
    $i 0$i sizeof($file); $i++) {

            
    $path pathinfo(APP_LIB_PATH $file[$i]);

            
    $dir_name   $path["dirname"];
            
    $class_file $path["basename"];
            
    $class_name basename($path["basename"], ".php");
            
            if (
    $type == "lib") {
            
              if (
    $key array_search($class_name$this->__lib)) {
            
                require_once 
    APP_LIB_PATH $class_file;
              
                if (
    class_exists($class_name)) {

                  
    $str = (string) $key;
                  
    $this->$str = new $class_name;
                }
              }
            }
            else {

              foreach (
    $load[$type] as $dir => $files) {
              
                for (
    $j 0$j sizeof($files); $j++) {
                
                  require_once 
    APP_ROOT_PATH $dir "/" $files[$j];
                }
              }
            }
          }
        }
      }
      
      
    /**
       *  Get all files and under lib directory (no subdirs)
       *
       *  @access  private
       *  @param   none
       *  @return  (array) || (bool) array with all the files under this dir or false
       */
      
    function __get_lib_files() {
      
        if (
    $handle opendir(APP_LIB_PATH)) {

          while (
    false !== ($file readdir($handle))) {

            if (
    $file != "." && $file != "..") {

              if (!
    is_dir(APP_LIB_PATH $file)) {

                
    $files[] = $file;
              }
            }
          }

          
    closedir($handle);

          return 
    $files;
        }

        return 
    false;
      }
      
      
    /**
       *  Get all files and under root directory and its subdirs
       *
       *  @access  private
       *  @param   (string) dir     directory name
       *  @param   (array) files    array with files
       *  @return  (array) || (bool) array with all the files under this dir or false
       */
      
    function __get_root_files($dir ""$files NULL) {

        if (
    $handle opendir(APP_ROOT_PATH $dir)) {

          while (
    false !== ($file readdir($handle))) {

            if (
    $file != "." && $file != "..") {

              if (!
    is_dir(APP_ROOT_PATH $file)) {

                
    $files[$dir][] = $file;
              }
              else {

                
    $this->__get_root_files($file, &$files);
              }
            }
          }

          
    closedir($handle);

          return 
    $files;
        }

        return 
    false;
      }
    }


    ?>


  2. #2
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What version of PHP are you using though?

    Do I have to assume your using PHP4.x? If so... Move to PHP5.0.5 or higher, then you can easily use __autoload()

  3. #3
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Lets assume, he is using PHP4.X and CAN NOT move to PHP5,

    what is your opinion then?
    ---------------------------
    Errors = Improved Programming.
    My Site

  4. #4
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Include on demand. Put a require_once on the top of each file, to include the files needed, rather than loading it all in advance. PHP processes are very short-lived, so it can easily drain performance to load things in advance, as you normally would in other programming languages.

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might also want to have a look at ezComponents - they have a nifty package-loader as part of the framework.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •