xbase Posted February 15, 2007 Share Posted February 15, 2007 Here is my db class: $conn = new database_conn(); $dbh = $conn->db; here is my class to create the top nav: class top_nav { var $i; var $l; function build() { global $hostname; global $dbh; $l = chr($this->i); $valid = $dbh->getOne("SELECT * FROM md_models WHERE alias REGEXP '^($l)'"); // Print valid letters if ($valid!=0) { $letter = '<td width=20 align=center> <a href="index.php?cmd=find&l='.$l.'" style="color: black; text-decoration: none; font-weight: bold">'.$l.'</a> </td>'; } else { $letter = '<td width=20 align=center style="color: #CCCCCC; font-weight: bold">'.$l.'</td>'; } return $letter; } } how come this is not working and I get an invalid object for $valid = $dbh->getOne. I use PHP 4.4.3 (not my choice). Any ideas? Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted February 16, 2007 Share Posted February 16, 2007 Have you tried creating a db object in your class, rather than using the global? I'm not sure, but it could be a scope issue. Quote Link to comment Share on other sites More sharing options...
utexas_pjm Posted February 16, 2007 Share Posted February 16, 2007 Every time someone uses global variables in a class God kills a puppy. Pass object instances to your class through the constructor. $conn = new database_conn(); $dbh = $conn->db; $nav = new top_nav($dbh); class top_nav { var $i; var $l; var $dbh; function top_nav($dbh) { $this->dbh = $dbh; } function build() { $l = chr($this->i); $valid = $this->dbh->getOne("SELECT * FROM md_models WHERE alias REGEXP '^($l)'"); // Print valid letters if ($valid!=0) { $letter = '<td width=20 align=center> <a href="index.php?cmd=find&l='.$l.'" style="color: black; text-decoration: none; font-weight: bold">'.$l.'</a> </td>'; } else { $letter = '<td width=20 align=center style="color: #CCCCCC; font-weight: bold">'.$l.'</td>'; } return $letter; } } Best, Patrick Quote Link to comment Share on other sites More sharing options...
emehrkay Posted February 16, 2007 Share Posted February 16, 2007 i just asked about this. i stored the instance of the class in a session var so that i only have to initialize it once $_SESSION['var'] = new class(); the thing i discovered is that if you do it this way, you need to include the class file before you do session_start(); then if you want to use a method anywhere in your scripts, just do $_SESSION['var']->method(); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.