Hi,

Tuesday, March 4, 2003, 8:53:25 PM, you wrote:
CB> Mh...i need this for a class loader.
CB> Could you post your class loader? (i do not want to invent the wheel again)
CB> Thanks!

This works for me to load modules in a base class so adapt it as you
need.

<?
class loaderClass {
        var $class_dir;
        var $classes;
        function loaderClass($class_dir=''){
                //you will need to repace $_SERVER['PHPINCDIR'] with your default inc 
dir as this is not a normal
                // $_SERVER variable :)
                $class_dir = (empty($class_dir))? $_SERVER['PHPINCDIR'] :$class_dir;
                $class_dir .= (substr($class_dir,-1) == '/')? '':'/'; // need an 
ending slash
                $this->class_dir = $class_dir;
        }
        // This searches the  class_dir set in constructor then
        //php.ini path for the class to load
        //All my classess are .inc
        function setPath($class){
                $r = false;
                //first check with this path
                $file = $this->class_dir.$class.'.inc';
                if(!file_exists($file)){
                        $paths = split(':',ini_get('include_path'));
                        $found = false;
                        while(!$found && list($key,$val) = each($paths)){
                                $sep = (PHP_OS == 'Windows')? '\\':'/';
                                $file = $val.$sep.$class.'.inc';
                                if($found = file_exists($file)){
                                        $r = $file;
                                }
                        }
                }else{
                        $r = $file;
                }
                return $r;
        }
        //$name is used to id the loaded class
        function &load($class,$name){
                $r = False;
                if(isset($this->classes[$name])){
                        $r =& $this->classes[$name];
                }else{
                        if($file = $this->setPath($class)){
                                $vars = '$this->classes[$name] =& new '.$class.'(';
                                $num_args = func_num_args();
                                if($num_args > 2) {
                                        $arg_list = func_get_args();
                                        for ($i = 2; $i < $num_args; $i++) {
                                        $vars .= ($i > 2)? ',':'';
                                        $vars .= (is_string($arg_list[$i]))? 
"'".$arg_list[$i]."'" : $arg_list[$i];
                                        }
                                }
                                $vars .= ');';
                                include_once($file);
                                eval($vars);
                                $r =& $this->classes[$name];
                        }
                }
                return $r;
        }
        function &isLoaded($name) {
                $r = False;
                if(isset($this->classes[$name])){
                        $r =& $this->classes[$name];
                }
                return $r;
        }
}

//usage for class foo2 called foo2Class in foo2Class.inc
include('loaderClass.inc');
class foo {
        var $loader;
        var $foo2;
        function foo(){
                this->loader =& new loaderClass('path_to_modules'); 
        }
        function load($var1,$var2,$var3){
                $this->foo2 =& 
$this->loader->load('foo2Class','foo2',$var1,$var2,$var3)
        }
}

$f = new foo();
$f->load('Hello',2,3)



-- 
regards,
Tom


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

Reply via email to