Jump to content

[SOLVED] Quick OOP question


Flames

Recommended Posts

I recently learned OOP and then made a DB class to handle connections etc, and there is a public variable called $con which is true if the database is connected.

I have another class which extends my DB class but I seem to be having problems with accessing the $con variable to see if it is connected, how can I do this, I tried using DB::$con but that gave "Fatal error: Access to undeclared static property: Database::$con"

 

Thanks

 

 

Link to comment
Share on other sites

I recently learned OOP and then made a DB class to handle connections etc, and there is a public variable called $con which is true if the database is connected.

I have another class which extends my DB class but I seem to be having problems with accessing the $con variable to see if it is connected, how can I do this, I tried using DB::$con but that gave "Fatal error: Access to undeclared static property: Database::$con"

 

Thanks

 

Do this:

 

public static $conn;

 

Does this help?

 

Link to comment
Share on other sites

class Database
{

private $db_host = ''; 
private $db_user = ''; 
private $db_pass = ''; 
private $db_name = ''; 
public static $con;

public function connect()
    {
        if(!$this->con)
        {
            $myconn = @mysql_connect($this->db_host,$this->db_user,$this->db_pass);
            if($myconn)
            {
                $seldb = @mysql_select_db($this->db_name,$myconn);
                if($seldb)
                {
                    $this->con = true; 
                    return true; 
                } else
                {
                    return false; 
                }
            } else
            {
                return false; 
            }
        } else
        {
            return true; 
        }
    }

public function disconnect()
{
	if($this->con)
	{
		if(@mysql_close())
		{
			 $this->con = false; 
			 return true; 
		}
		else
		{
			 return false; 
		}
	}
}	
  }

 

class Login extends Database
{
public function checkDatabase($field, $check)
{
if(Database::$con)
{
 $query = "SELECT * FROM Users where $field = '$check'";
 $result = mysql_query($query) or die("error");;
 if(mysql_num_rows($result)>0)
	{
	 return "no";
	}
	else
	{
	 return "yes";
	}
}
else
{
return "not connected";
}
}
}

Link to comment
Share on other sites

I see the db informations (the private variables) are blank in the class. How are setting those? Directly in the class? If not, create a constructor in the class to set those and then use those in the connect() method. As they are blank in the class, you get "false" from the connect() method.

 

<?php
/**
* Database.php
*/
class Database
{

private $db_host = ''; 
private $db_user = ''; 
private $db_pass = ''; 
private $db_name = ''; 
public static $con;

/**
* Magic construct method
*/
public function __construct($host, $user, $password, $dbname) {
  $this->$db_host = $host;
  $this->db_user = $user;
  $this->db_pass = $password;
  $this->db_name = $dbname;
}

public function connect()
    {
        if(!$this->con)
        {
            $myconn = @mysql_connect($this->db_host,$this->db_user,$this->db_pass);
            if($myconn)
            {
                $seldb = @mysql_select_db($this->db_name,$myconn);
                if($seldb)
                {
                    $this->con = true; 
                    return true; 
                } else
                {
                    return false; 
                }
            } else
            {
                return false; 
            }
        } else
        {
            return true; 
        }
    }

public function disconnect()
{
	if($this->con)
	{
		if(@mysql_close())
		{
			 $this->con = false; 
			 return true; 
		}
		else
		{
			 return false; 
		}
	}
}	
  }
?>

 

To call this class:

 

<?php
/**
* dbtest.php
*/
include('Database.php');
$db = new Database('localhost', 'username', 'password', 'database_name');
var_dump($db->connect());
?>

 

Link to comment
Share on other sites

/* Add to database class... */
protected function isConnected()
{
   return $this->con;
}

/* Login Class */

class Login extends Database
{
public function checkDatabase($field, $check)
 {
 if(parent::isConnected())

 

Link to comment
Share on other sites

Well, for one, your DB connect method is not written correctly.

 

By the way, you can connect without selecting a DB. Not sure of your logic there.

public function connect()
    {
        if(!self::$con)
        {
            $myconn = @mysql_connect($this->db_host,$this->db_user,$this->db_pass);
            if($myconn)
            {
                $seldb = @mysql_select_db($this->db_name,$myconn);
                if($seldb)
                {
                    self::$con = true;
                    return true; 
                }
            }
            self::$con = false;
            return false;
        } else
        {
            return true; 
        }
    }

 

After you did that, replace

if(Database::$con)

 

With

if(parent::connect())

 

Since your DB connect method returns a boolean, you can just do that instead of accessing member data publicly. This way, you can set the $con var in DB to private. :)

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.