TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Problem using one class within another - class v instance (http://www.talkphp.com/advanced-php-programming/5415-problem-using-one-class-within-another-class-v-instance.html)

JohnAtYM 05-17-2010 06:40 PM

Problem using one class within another - class v instance
 
For reasons that probably don't bear scrutiny, I've begun an application using a class to handle mySQL access and have been trying to use it within other classes without much success, as it appears I'm trying to access an object that's not instantiated. If I can be pointed to a good example of this I'd be grateful as most of the tutorials etc I've found aren't the same context.

Near the beginning of my application logic is:
Code:

        $site = new cache_site;

        if (isset($_GET["start_add"])) {
                if ($site->get_by_url($_GET["start_add"])) {

and the class definition it's using begins
Code:

class cache_site {
        public $id;
        public $name;
        public $start_url;
        public $last_cache;
        public $fetch_status;
        public $language;
       
    private $db;
    public function __construct() {
      $this->db = DB::getInstance();
    }
       
        public function get_by_url($url) {
                $sql = "SELECT * FROM ".TB_CACHE_SITE." WHERE start_url = '".$url."'";
                if ($site = $this->db->get_results($sql)) {
                        $this->fetch_status = true;
                        $this->id = $site->site_id;

The instantiation of $site goes ok but the call to get_by_url gets a fatal error on
Code:

if ($site = $this->db->get_results($sql)) {
"Call to a member function get_results() on a non-object"

Am I addressing it wrongly or have I got totally the wrong end of the stick?

The database access class (which I downloaded & then tried to adapt) now begins like this:
Code:

class DB {
        public function __construct()  { }
        public function __clone()  {  }
               
        private static $dbh = NULL;
        public static function getInstance() {
                if (self::$dbh === NULL) {
                        self::$dbh = new self;
                        self::connect();
                }
                return self::$dbh;
        }

        public static function connect() {
        //does the connection using self::functions/variables
        }

        function get_results($query=null, $output = OBJECT) {
        //gets the results of a query using this->functions/variables
        }


JohnAtYM 05-18-2010 11:05 AM

OK - the code I posted works. The problem was in the function connect() which was overwriting the dbh instance with the return from the mysql connect. So, for the sake of completeness, here's the fixed version of the bit that was producing the error:
Code:

private static $dbc; // mysql connection
private static function connect()
{
        self::$dbc = @mysql_connect(self::$dbhost,self::$dbuser,self::$dbpassword);
               
        if ( ! self::$dbc )
        {
                // error handling
        }
        if ( !@mysql_select_db(self::$dbname,self::$dbc))
        {
                // more error handling
        }
}

Good thread on this already: http://www.talkphp.com/advanced-php-...n-pattern.html


All times are GMT. The time now is 05:14 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0