RyanMinor Posted November 12, 2011 Share Posted November 12, 2011 Hi all. I have a singleton database class that just basically extends PDO. I then created a file called configure.php that will be included on every single page in my small application. In this file, I get the database instance and also instantiate a Site class that returns information about the site, pages, etc. Well, for some reason my Site class is not reading the $database variable that is defined in the configure.php file. I am getting the following error: "Notice: Undefined variable: database in C:\xampp\htdocs\individualbasic\libraries\Site.php on line 30 Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\individualbasic\libraries\Site.php on line 30". Below are my files. Thanks in advance. Database.php <?php class Database extends PDO { public static $_instance; public function __construct() { try { parent::__construct(DB_TYPE . ':host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS); } catch (PDOException $e) { $this->error = $e->getMessage(); } } public static function getInstance() { if (!self::$_instance) { self::$_instance = new Database(); } return self::$_instance; } public function __destruct() { // I want to close the connection here. } } Site.php <?php class Site { /** * Checks the validity of the site token with the token in the MinorCorp database. */ public function __construct() { /*try { $query = $database->prepare("SELECT individual_site_url, individual_site_token FROM individual_site WHERE individual_site_url = ? AND individual_site_token = ? AND individual_site_active = 1"); $result = $query->execute(array(SITE_URL, SITE_TOKEN)); if (count($result) == 1) { return TRUE; } die('Your website is not valid.'); } catch (Exception $e) { echo $e; }*/ } /** * Retrieves vital information about each page. */ public function page($page) { try { $query = $database->prepare("SELECT page_title, page_keywords, page_description, page_content, page_image FROM page WHERE page_name = ?"); $query->execute(array($page)); return $query->fetch(); } catch (Exception $e) { echo $e; } } /** * Retrieves bottom advertisements from the MinorCorp database. */ public function bannerAd() { try { } catch (Exception $e) { echo $e; } } /** * Retrieves vital information about each page. */ public function sidebarAd() { try { } catch (Exception $e) { echo $e; } } } configure.php (This is the file that is included on every page) <?php include_once('configuration/configuration.php'); require_once('libraries/Database.php'); $database = Database::getInstance(); require_once('libraries/Site.php'); $site = new Site(); $pageInfo = $site->page($page); $bottomAd = $site->bannerAd(); $sidebarAd = $site->sidebarAd(); Link to comment https://forums.phpfreaks.com/topic/251001-having-some-problems-with-a-few-classes-i-wrote/ Share on other sites More sharing options...
RyanMinor Posted November 12, 2011 Author Share Posted November 12, 2011 Never mind I think I figured it out. I just went through the principles of variable scope in my head. Sorry for wasting a post. Link to comment https://forums.phpfreaks.com/topic/251001-having-some-problems-with-a-few-classes-i-wrote/#findComment-1287620 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.