|
-
March 24th, 2009, 09:34 PM
#1
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,
-
March 25th, 2009, 10:45 AM
#2
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(); } } ?>
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|