Re[2]: Best way to use other objects within classes?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

Here it is for anyone that would like to try it.


<?php

if(preg_match('/^win/i',PHP_OS)){
  define('OS_SEP','\\');
  define('OS_JOIN',';');
}else{
  define('OS_SEP','/');
  define('OS_JOIN',':');
}
class classLoader {
  function loadLib($library=''){
    if(!empty($library)){
      $file = $GLOBALS['class_dir'].$library.'.lib'; 
      if(!file_exists($file)){
        $paths = split(OS_JOIN,ini_get('include_path'));
        $found = false;
        while(!$found && list($key,$val) = each($paths)){
          $file = $val.OS_SEP.$library.'.lib';
          $found = file_exists($file);
        }
      }
      if($found) include($file);
    }
    $array = get_declared_classes();
    foreach($array as $classname){
      if(!isset($GLOBALS['loaded_classes'][$classname])) $GLOBALS['loaded_classes'][$classname] = array();
    }
    if(isset($start_classes) && is_array($start_classes)){
      foreach($start_classes as $class=>$info){
        $class_name = strtolower($class);
        $name = $info['name'];
        $hash = mhash(MHASH_TIGER,serialize($info['vars']));
        if(isset($GLOBALS['loaded_classes'][$class_name])){
          $varcount = count($info['vars']);
          $vars = '$GLOBALS[\'loaded_classes\'][$class_name][$name][\'class\'] =& new '.$class.'(';
          if($varcount > 0){
            for ($i = 0; $i < $varcount; $i++) {
              $vars .= ($i > 0)? ',':'';
              $varname = 'variable'.$i;
              $$varname = $info['vars'][$i];
              $vars .= "\$$varname";
            }
          }
          $vars .= ');';
          eval($vars);
          $GLOBALS['loaded_classes'][$class_name][$name]['hash'] = $hash;
          if(!empty($info['global'])) $GLOBALS[$info['global']] =& $GLOBALS['loaded_classes'][$class_name][$name]['class'];
        }
      }
      unset($start_classes);
    }
  }
  function &load($class,$name=''){
    $class_name = strtolower($class);
    $name = (empty($name))? $class:$name;
    $num_args = func_num_args();
    $arg_list = func_get_args();
    $hash = mhash(MHASH_TIGER,serialize($arg_list));
    if(isset($GLOBALS['loaded_classes'][$class_name][$name])){
      if($num_args > 2 && $GLOBALS['loaded_classes'][$class_name][$name]['hash'] != $hash){
        $vars = '$GLOBALS[\'loaded_classes\'][$class_name][$name][\'class\']->'.$class.'(';
        for ($i = 2; $i < $num_args; $i++) {
          $vars .= ($i > 2)? ',':'';
          $varname = 'variable'.$i;
          $$varname =& $arg_list[$i];
          $vars .= "\$$varname";
        }
        $vars .= ');';
        eval($vars);
        $GLOBALS['loaded_classes'][$class_name][$name]['hash'] = $hash;
      }
      return $GLOBALS['loaded_classes'][$class_name][$name]['class'];
    }elseif(!isset($GLOBALS['loaded_classes'][$class_name])){
      $file = $GLOBALS['class_dir'].$class.'.inc'; 
      if(!file_exists($file)){
        $paths = split(OS_JOIN,ini_get('include_path'));
        $found = false;
        while(!$found && list($key,$val) = each($paths)){
          $file = $val.OS_SEP.$class.'.inc';
          $found = file_exists($file);
        }
      }else $found = true;
      if($found){
        include($file);
        classLoader::loadLib();
      }
    }
    if(isset($GLOBALS['loaded_classes'][$class_name])){
      $vars = '$GLOBALS[\'loaded_classes\'][$class_name][$name][\'class\'] =& new '.$class.'(';
      if($num_args > 2) {
        for ($i = 2; $i < $num_args; $i++) {
          $vars .= ($i > 2)? ',':'';
          $varname = 'variable'.$i;
          $$varname =& $arg_list[$i];
          $vars .= "\$$varname";
        }
      }
      $vars .= ');';
      eval($vars);
      $GLOBALS['loaded_classes'][$class_name][$name]['hash'] = $hash;
      return $GLOBALS['loaded_classes'][$class_name][$name]['class'];
    }
    return false;
  }
  function remove($class,$name=''){
    $r = false;
    $class_name = strtolower($class);
    $name = (empty($name))? $class:$name;
    if(isset($GLOBALS['loaded_classes'][$class_name][$name])){
      unset($GLOBALS['loaded_classes'][$class_name][$name]);
      $r = true;
    }
    return $r;
  }
  function &find($class,$name){
    $class_name = strtolower($class);
    if(isset($GLOBALS['loaded_classes'][$class_name][$name])){
      return $GLOBALS['loaded_classes'][$class_name][$name]['class'];
    }else{
      return false;
    }
  }
}
//Usage
// not tested in php5
// all class files end in .inc
// all library files end in .lib
// I don't instanciate the class so uou just
// need to set 2 global variables them call loadLib()
// without any args to get a list of already loaded classes.

$class_dir = '/usr/local/apache/phpinc';
$loaded_classes = array();
classLoader::loadLib();

// The load a class giving it a name and any args that it needs,
// it can pass references without problem
// for example I have a Mysql table class I use just about everywhere
// which needs the db and table to use


$table =& classLoader::load('tableClass','members','testdb','test_table');

// now I can get that same instance by 2 methods
// simply repeat the line with the same instance name or
// use the find() method if I know it already exists.

$table2 =& classLoaderFind('tableClass','members');

//if you need a seperate instance of a class give it a new name

$table3 =& classLoader::load('tableClass','members2','testdb','test_table');

//You can use loadLib() to setup a bunch of base classes that are always needed

classLoader::loadLib('base');

//Al this works inside class definitions as well.

class a {
  var $table;
  function a(){
    $this->table =& classLoader::load('tableClass','members','testdb','test_table');
  }
}



-- 
regards,
Tom

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux