Jump to content

OOP Database Issues


xbase

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.