Doing some late-night programming when 💥
Fatal error: Cannot redeclare class database in /home/www/figmentenygma.com/3_0/app/blogger/shr/dal/drv/mysql_drv.php on line 2
stopped me dead in my tracks
But it was right, I suppose I had. I guess it is a matter of scope, which I seem to be misunderstanding, and is the basis for my question and post.
The PHP page in question 'categories.php' looks like this:
<?php
require_once("../shr/inc/header_inc.php");
require_once("../shr/inc/database_inc.php");
require_once("../shr/cls/blogger_class.php");
require_once("../shr/inc/category_menu_inc.php");
$oBlogger = new Blogger($aConfiguration['LOCATION']['install_path']);
if (!is_null($_GET['sMode'])) {
require_once("../shr/inc/category_".strtolower($_GET['sMode'])."_inc.php");
}
require_once("../shr/inc/footer_inc.php");
?>
Now here is a copy of the first file that declares the 'database' class: '../shr/inc/database_inc.php'
<?php
if (file_exists($aConfiguration['LOCATION']['install_path']."shr/dal/dal_init.php")) {
require_once($aConfiguration['LOCATION']['install_path']."shr/dal/dal_init.php");
if ($bDriverSuccess == true) {
$oDB = new Database;
$_DATASTREAM = $oDB->connect($aConfiguration['DATABASE']['host'], $aConfiguration['DATABASE']['user'], $aConfiguration['DATABASE']['pass']);
if ($oDB->select_db($aConfiguration['DATABASE']['name'])) {
$bDatabaseSuccess = true;
}
}
}
?>
And here is the second: '../shr/cls/blogger_class.php'
class Blogger {
function Blogger($sInstall_Location)
{
if (is_readable($sInstall_Location."configuration_ini.php")) {
$aConfiguration = parse_ini_file($sInstall_Location."configuration_ini.php", true);
require($aConfiguration['LOCATION']['install_path']."shr/dal/dal_init.php");
}
$this->oDB = new Database;
$this->Datastream = $this->oDB->connect($aConfiguration['DATABASE']['host'], $aConfiguration['DATABASE']['user'], $aConfiguration['DATABASE']['pass']);
$this->sAboutTable = $aConfiguration['TABLES']['about'];
$this->sBlogTable = $aConfiguration['TABLES']['entries'];
$this->sResponseTable = $aConfiguration['TABLES']['comments'];
$this->sCategoryTable = $aConfiguration['TABLES']['categories'];
$this->sUserTable = $aConfiguration['TABLES']['users'];
$this->sSetupTable = $aConfiguration['TABLES']['setup'];
if ($rSetupInfo = $this->oDB->query("SELECT * FROM ".$this->sSetupTable)) {
$this->aSetupInfo = $this->oDB->fetch_array($rSetupInfo);
}
return $this->aSetupInfo;
}
}
the file - ' $aConfiguration['LOCATION']['install_path']."shr/dal/dal_init.php" ' contains simply this:
<?php
if (file_exists($aConfiguration['LOCATION']['install_path']."shr/dal/drv/".$aConfiguration['DATABASE']['type']."_drv.php")) {
require($aConfiguration['LOCATION']['install_path']."shr/dal/drv/".$aConfiguration['DATABASE']['type']."_drv.php");
$bDriverSuccess = true;
}
?>
and of course the file *_drv.php contains the actual Database class istelf, more or less its my kludgy version of a Database Abstraction Layer that I hope to build up over time.
Now to the meat of the question:
In regards to the first file categories.php and the Blogger class, I only get this error when I uncomment line '6' which is
$oBlogger = new Blogger($aConfiguration['LOCATION']['install_path']);
I am most likely wrong, but I was under the impression that the scope of objects and functions and classes were limited to within the class/function that they were declared in. So if i have an object that declares itself a new Database class in database_inc.php, and one that is within a constructor of the Blogger class, shouldnt they NOT see each other?
Is there a workaround for this? The way I have things set up this might be a temporary showstopper, as this is part of the admin back-end for my application, meaning i need database access outside of the Blogger class on the back-end,and the Blogger class gets used extensivly on the front end where i dont want to have all kinds of database code, I am hoping to maintain an 'independence' of this application/class from the other code, so it can be portable between many projects.
HELP!
Thank you 🙂