Page 1 of 1

accessing object of my connection class

Posted: Thu Oct 21, 2010 12:14 pm
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;
		}
		
		
	}
}

Re: accessing object of my connection class

Posted: Thu Oct 21, 2010 2:51 pm
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;
                }
               
               
        }
}

Re: accessing object of my connection class

Posted: Thu Oct 21, 2010 5:13 pm
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;
                }
       
       
        }
}

Re: accessing object of my connection class

Posted: Tue Oct 26, 2010 5:26 am
by quickLearner
thank you cpetercarter and TipPro i followed your suggestion and i have been able to accomplish my task.