I have a class which I’ll like to include couple db routines instead of inline.
class Classica{
public $dbhost;
public $dbname;
public $dbuser;
public $dbpass;
__function construct(){
//hook connectdb() upon contruct.
}
#connect to database
public function connectdb($dbhost,$dbname,$dbuser,$dbpass){
$link = mysql_connect($dbhost,$dbuser,$dbpass);
if (!$link) {
die('Could not connect: ' . mysql_error());
}else {
//echo 'Connected Successfully to Database<br>';
}
@mysql_select_db($dbname) or die( "Unable to select database");
}
#read database
function readdb(){
}
#update database
private function updatedb(){
}
#close database connection
function closedb(){
mysql_close();
}
}
config.php
$dbhost = 'localhost';
$dbname = 'dem';
$dbuser = 'root';
$dbpass = '';
index.php
<?php
error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE);
session_start();
include('config.php');
include('classica.php');
$obj = new classica();
?>
The database connection details I will like to maintain on a separate config file which is included on document load.
The problems I’m having:
Retrieving database config values inside the class.
Trying to attach a separate database class to separate from parent.
Any suggestions of approach and/or solution.
The simplest method would be do
include('config.php')inside the connectdb function (or the function calling it). When youincludea PHP file (remember to include<?phpopen tag!), the variables will become declared within the scope of the function doing the inclusion. So for example, __construct() (note that you have a typo) may be: