Re[2]: [PHP] Best way to use other objects within classes?
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
Re[3]: [PHP] Best way to use other objects within classes?
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()
Re: [PHP] Best way to use other objects within classes?
Hi, Friday, June 3, 2005, 5:16:19 AM, you wrote: MP Hi All, MP I'm using the MDB2 object to access my MySQL database. I have a number MP of classes that perform page-building activities that need db access, MP and I'm wondering what the best way to expose the MDB2 object to them is? MP (Note: my development environment is PHP 5.0.3, but the production MP environment is 4.3.10. This is my first project built with 5.x local and MP 4.1.x remote, so if anyone with more experience spots any fatal flaws MP where I'm using 5.x specific methods etc, I'd appreciate knowing about them) MP Currently, I'm instantiating the MDB2 class in my main page, then MP passing it as an object reference to each of the other classes that MP require it. MP A simple representation of my main page and a class would be: MP ? MP $db = connectMDB2(); // function that returns instantiated MDB2 object MP $comments = new displayComments(); // class that performs displaying MP of comments MP $comments-set_db($db); // passing the MDB2 object to the class MP $comments-doSomethingElse(); ? MP The displayComments() class might then look something like: MP class displayComments{ MP private $db; MP public function set_db($db){ $this-db = $db; MP } MP public function doSomethingElse(){ MP $sql = SELECT something FROM sometable; $rs = $this-db-query($sql); MP while ($row = $rs-fetchRow(MDB2_FETCHMODE_OBJECT)){ echo $row-something.br /; MP } $rs-free(); MP } MP } MP My main page calls on at least 8 or 9 such classes as it is being built, MP and I'm concerned that I may not be handling the MDB2 object in respect MP to them in the most efficient way possible. In a way, I guess this isn't MP specifically about the MDB2 package, but more about how to handle MP objects when they are required within classes. MP I'd very much appreciate any comments or advice anyone might be able to MP give. MP Much warmth, MP Murray My solution to this problem was to write a class loader and to call it whenever I needed a particular class. I give each instance a name and the loader checks if an instance of that class, that name exists. If it does it returns a reference directly, if not it loads the class and then returns the reference. This way I don't have to worry about passing references around. It is a little more complex than that as I also check if the class was passed the same variables, if not it calls the constructor again. Let me know if you want to try the code (only tested on php4) -- regards, Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Best way to use other objects within classes?
Hi All, I'm using the MDB2 object to access my MySQL database. I have a number of classes that perform page-building activities that need db access, and I'm wondering what the best way to expose the MDB2 object to them is? (Note: my development environment is PHP 5.0.3, but the production environment is 4.3.10. This is my first project built with 5.x local and 4.1.x remote, so if anyone with more experience spots any fatal flaws where I'm using 5.x specific methods etc, I'd appreciate knowing about them) Currently, I'm instantiating the MDB2 class in my main page, then passing it as an object reference to each of the other classes that require it. A simple representation of my main page and a class would be: ? $db = connectMDB2(); // function that returns instantiated MDB2 object $comments = new displayComments(); // class that performs displaying of comments $comments-set_db($db); // passing the MDB2 object to the class $comments-doSomethingElse(); ? The displayComments() class might then look something like: class displayComments{ private $db; public function set_db($db){ $this-db = $db; } public function doSomethingElse(){ $sql = SELECT something FROM sometable; $rs = $this-db-query($sql); while ($row = $rs-fetchRow(MDB2_FETCHMODE_OBJECT)){ echo $row-something.br /; } $rs-free(); } } My main page calls on at least 8 or 9 such classes as it is being built, and I'm concerned that I may not be handling the MDB2 object in respect to them in the most efficient way possible. In a way, I guess this isn't specifically about the MDB2 package, but more about how to handle objects when they are required within classes. I'd very much appreciate any comments or advice anyone might be able to give. Much warmth, Murray -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Best way to use other objects within classes?
On 6/2/05, Murray @ PlanetThoughtful [EMAIL PROTECTED] wrote: private $db; PHP4 doesn't have member visibility. -- Greg Donald Zend Certified Engineer http://destiney.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Best way to use other objects within classes?
Greg Donald wrote: On 6/2/05, Murray @ PlanetThoughtful [EMAIL PROTECTED] wrote: private $db; PHP4 doesn't have member visibility. Hi Greg, Thanks for this tip! Regards, Murray -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php