Best way to use other objects within classes?

Best way to use other objects within classes?

am 02.06.2005 21:16:19 von lists

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."
";
}
$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: Best way to use other objects within classes?

am 02.06.2005 22:30:21 von Greg Donald

On 6/2/05, Murray @ PlanetThoughtful wrote:
> private $db;

PHP4 doesn't have member visibility.

--=20
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: Best way to use other objects within classes?

am 02.06.2005 23:12:43 von lists

Greg Donald wrote:
> On 6/2/05, Murray @ PlanetThoughtful 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

Re: Best way to use other objects within classes?

am 03.06.2005 04:15:38 von matthew

* "Murray @ PlanetThoughtful" :

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

> > $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;
> }

The above is the nut you need, and you're doing it right. As Greg
pointed out, you'll need to change that from 'private $db' to 'var $db'
as PHP4 doesn't have member visibility. However, you're correctly
passing by reference -- something required by PHP4 if you want to share
the same object amongst several classes. (In PHP5, objects are passed by
reference by default, and the above code will actually generate some
notices -- but since you're developing for PHP4, you can safely ignore
them).

--
Matthew Weier O'Phinney | WEBSITES:
Webmaster and IT Specialist | http://www.garden.org
National Gardening Association | http://www.kidsgardening.com
802-863-5251 x156 | http://nationalgardenmonth.org
mailto:matthew@garden.org | http://vermontbotanical.org

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

Re: Best way to use other objects within classes?

am 03.06.2005 08:54:39 von Tom Rogers

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."
";
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

Re: Best way to use other objects within classes?

am 03.06.2005 18:51:06 von Jason Barnett

Matthew Weier O'Phinney wrote:
> * "Murray @ PlanetThoughtful" :
>
>
>>(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)
>
>
>

If you're developing with PHP5, but the production environment is PHP4
then at the top of each script you probably want to add:


ini_set('zend.ze1_compatibility_mode', 1);

?>

This way you will have objects default to pass by value instead of by
reference and you don't end up deploying to production server with some
really hard-to-find dereferencing problems.


--
Teach a man to fish...

NEW? | http://www.catb.org/~esr/faqs/smart-questions.html
STFA | http://marc.theaimsgroup.com/?l=php-general&w=2
STFM | http://php.net/manual/en/index.php
STFW | http://www.google.com/search?q=php
LAZY |
http://mycroft.mozdev.org/download.html?name=PHP&submitform= Find+search+plugins

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

RE: Re: Best way to use other objects within classes?

am 03.06.2005 19:11:43 von lists

> If you're developing with PHP5, but the production environment is PHP4
> then at the top of each script you probably want to add:
>
> >
> ini_set('zend.ze1_compatibility_mode', 1);
>
> ?>
>
> This way you will have objects default to pass by value instead of by
> reference and you don't end up deploying to production server with some
> really hard-to-find dereferencing problems.

Hi Jason,

Not a bad tip, thank you! However, I've been, to the best of my knowledge,
explicit in passing by reference in my code wherever I've needed to access
objects within classes etc.

Many thanks,

Murray

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

Re: Re: Best way to use other objects within classes?

am 03.06.2005 19:23:34 von Jason Barnett

Then you're a better coder than me, but then again who isn't. :) It's
just that in general I try to make things as strict / difficult as
possible to code during development so that I will catch as many errors
as possible before hand. But then again I use error_reporting(E_ALL).

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

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

am 26.06.2005 04:27:44 von Tom Rogers

Hi,

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



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_tabl e');

// 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_tab le');

//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_tabl e');
}
}



--
regards,
Tom

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

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

am 26.06.2005 04:33:09 von Tom Rogers

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>
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_tabl e');

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_tab le');

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_tabl e');
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