accessing object of my connection class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
quickLearner
Forum Newbie
Posts: 3
Joined: Tue Sep 14, 2010 11:01 am

accessing object of my connection class

Post by quickLearner »

hello everyone, i am pretty new to this php stuff. i have a connection class which i have created to connect to my database and return an object of the connection. i now want to access that object in another class using it to run a query on my database to fetch items from it. here is the code snipet

this is the connection class:

Code: Select all

<?php
class connection{
	
	public $conn;
	public function connect()
	{
		// create connection object
		
		$conn = new mysqli(SERVER, DB_USER, USER_PWD, DB_NAME);
		
		// test connection
		if(mysqli_connect_error()){
			throw new Exception("connection failed");
		}else{
			return conn;
		}
	
	
	}
}

i now want to access the connection object in another class, how do i go about it. this is what i have done.

Code: Select all

class testify{
	private $db;
	public function __construct()
	{
		// create an object of the database class
		$db= new connection();
		$db->connect();
		
	}
	
	public function FetchTestimony()
	{
		$query = "SELECT * FROM test_list";
		$result = $this->db->conn->query($query);
		
		if(!$result){
			throw new Exception($query);
		}else{
			return $result;
		}
		
		
	}
}
Last edited by Weirdan on Thu Oct 21, 2010 12:29 pm, edited 1 time in total.
Reason: added syntax highlighting
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: accessing object of my connection class

Post by cpetercarter »

Something like this, perhaps?

Code: Select all

class testify{
        private $db;
        public function __construct()
        {
                // create an object of the database class
                $this->db= new connection();
                $this->db->connect();
               
        }
       
        public function FetchTestimony()
        {
                $query = "SELECT * FROM test_list";
                $result = $this->db->conn->query($query);
               
                if(!$result){
                        throw new Exception($query);
                }else{
                        return $result;
                }
               
               
        }
}
TipPro
Forum Commoner
Posts: 35
Joined: Wed Mar 15, 2006 6:39 pm

Re: accessing object of my connection class

Post by TipPro »

In your 'connect' function you return the database connection instead of assigning it to your object variable 'conn'

Code: Select all

<?php
class connection{
       
        public $conn;
        public function connect()
        {
                // create connection object
               
                $conn = new mysqli(SERVER, DB_USER, USER_PWD, DB_NAME);
               
                // test connection
                if(mysqli_connect_error()){
                        throw new Exception("connection failed");
                }else{
                        return conn;
                        //try this instead
                        //$this->conn  = $conn;
                }
       
       
        }
}
quickLearner
Forum Newbie
Posts: 3
Joined: Tue Sep 14, 2010 11:01 am

Re: accessing object of my connection class

Post by quickLearner »

thank you cpetercarter and TipPro i followed your suggestion and i have been able to accomplish my task.
Post Reply