first post here!
Okay so I have a function containing this code:
require_once('../strap.php');
$admin = new Member;
$admin->setName($_POST['username']);
$admin->setPassword($hash);
$admin->setEmail($_POST['email']);
$admin->updateDatabase();
the required file looks like this:
//define constants
define('ROOTPATH', __DIR__ . '/');
define('CLASSES', __DIR__ . '/classes/');
//Lazy load any required php classes
function __autoload($class_name) {
$file = CLASSES . $class_name . '.php';
if( file_exists($file) ) {
require_once $file;
} else {
echo "Can't find ". $file;;
}
}
//does config exist?
if (!file_exists('config.php')) {
//redirect to install routine if config does not exist
header(ROOTPATH . 'admin/install.php') ;
}
require('config.php');
//connect to the database
try {
$pdo = new PDO('mysql:database='.$conf['database'].';host='.$conf['host'].';port='.$conf['port'].';',
$conf['username'], $conf['password']);
} catch(PDOException $e) {
die('ERROR: Could not connect: ' . print_r($e->getMessage()));
}
and the required file in that (config.php) looks like this:
$conf['username'] = 'root';
$conf['password'] = '';
$conf['host'] = 'localhost';
$conf['database'] = 'dddatabase';
$conf['port'] = '3306';
$conf['prefix'] = 'cms_';
My problem is that I get this error: Notice: Undefined variable: conf in D:\wamp\www\testing\scripts\CMS\classes\Member.php on line 152
and also
Notice: Undefined variable: pdo in D:\wamp\www\testing\scripts\CMS\classes\Member.php on line 153
When it initialises a new Member object and then calls the updateDatabase() method, which uses the $conf[] array and the $pdo object.
however if I include strap.php again, I get the error: Cannot redeclare __autoload() (previously declared in D:\wamp\www\testing\scripts\CMS\strap.php:16) in D:\wamp\www\testing\scripts\CMS\strap.php on line 23
Does anyone have any idea what is going wrong here?
In your Member class, where you try to access the $pdo, and $conf variables, you need to declare them as global. So inside the Member class, in the function you are trying to use these variables, add the following to the beginning of the function (or at least before you are referencing them):