Jump to content

Having some problems with a few classes I wrote


RyanMinor

Recommended Posts

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.