View Single Post
Old 05-17-2010, 06:40 PM   #1 (permalink)
JohnAtYM
The Visitor
 
Join Date: May 2010
Location: sarf Landin
Posts: 3
Thanks: 0
JohnAtYM is on a distinguished road
Images 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 is offline  
Reply With Quote