TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-17-2010, 06:40 PM   #1 (permalink)
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
Old 05-18-2010, 11:05 AM   #2 (permalink)
The Visitor
 
Join Date: May 2010
Location: sarf Landin
Posts: 3
Thanks: 0
JohnAtYM is on a distinguished road
Default

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: How to use the Singleton design pattern

Last edited by JohnAtYM : 05-18-2010 at 11:34 AM. Reason: supplemental link
JohnAtYM is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the Singleton design pattern Karl Advanced PHP Programming 27 10-22-2012 08:16 AM
storing class instance into session nizzy Advanced PHP Programming 3 10-02-2008 06:49 AM
[Tutorial] Basic tutorial about class basics Tanax Absolute Beginners 14 07-24-2008 01:37 PM
PHP5 Classes A to Z Part 1 quantumkangaroo Advanced PHP Programming 11 04-01-2008 04:21 AM
Tutorial: PHP and OOP, a beginners guide Village Idiot Tips & Tricks 0 09-06-2007 04:23 PM


All times are GMT. The time now is 01:45 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design