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

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

 



Hi,

Sunday, June 26, 2005, 12:27:44 PM, you wrote:
TR> Hi,

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


TR> <?php

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

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

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


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

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

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

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

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

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

TR> classLoader::loadLib('base');

TR> //Al this works inside class definitions as well.

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



TR> -- 
TR> regards,
TR> Tom


should do this when fully awake..
that should be

$table2 =& classLoader::find('tableClass','members');

-- 
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