How to create a global variable of type ?.class.php [PHP5]
I'm trying to create a global variable ($db) of type Database (as shown below) to be accessed within Tools class.
I'm using PHP5 and running into a little problem... I have 2 files:
- Database.class.php
- Tools.class.php
The contents look as follows:
Code:
<?php
require 'Database.class.php';
class Tools
{
public $db = ""; // is this the problem?
function Tools($server, $user, $password, $database)
{
$db = new Database($server, $user, $password, $database);
}
public function Run()
{
$db->Connect();
}
}
?>
Code:
<?php
class Database
{
function Database($server, $user, $password, $database)
{
// DO SOMETHING HERE //
}
public function Connect()
{
// DO SOMETHING HERE //
}
For some odd reason this doesn't seem to work ... When I am in Tools.class.php I am trying to access the Database public variable ($db) to run ($db->Connect()) as follows:
$db->Connect();
But ... as you may have guessed ... this doesn't work...
Any clues/hints would be much appreciated.
Thanks,
Re: How to create a global variable of type ?.class.php [PHP5]
Try using the following instead.
PHP Code:
<?php
require 'Database.class.php';
class Tools {
public $db = "";
function Tools($server, $user, $password, $database) {
$this->$db = new Database($server, $user, $password, $database);
}
public function Run() {
$this->$db->Connect();
}
}
?>