|
Posted by Slant on 09/02/06 20:59
Here's a question that most will have different answers to. I'm just
dying to find a solution that seems halfway automated!! There really
are two seperate issues which might be answered by the same answer.
Web App:
- constants.php begin -
define("DB_HOST", "locahost");
.... to include the standard connection info
define("DB_NAME", "mydatabase");
- constants.php end -
- classes.php begin -
class Database {
function __construct() {
$link = mysql_connect(DB_HOST,DB_USER,DB_PASS);
mysql_select_db(DB_NAME,$link);
}
function getData() {
$query = ...
$result = ...
while (...) {
$this->data['name'] = $row['name'];
...
}
}
}
class Stuff {
function __construct() {
$database = new Database <---- this does not seem to work
}
function getData() {
$database->getData();
$this->data = $database->data;
}
}
- classes.php end -
>From here, I can call the "data" field of the Stuff object with
$stuff->data.
I get: "Fatal error: Call to a member function getData() on a
non-object in ..." (it refers to the call in "Stuff | getData". So it
is obviously not seeing the Database object from within the Stuff
class.
So here are the questions (which many of you may have already spotted):
1) How can I get the Database class to recognize the constant db
credentials?
2) How can I get the Link class to call a method in the Database class
without having to automatically call "$database = new Database;" right
after the database class is written, then calling "global $database" in
the Link class. I HATE having to do that.
Any thoughts? I've seen many methods for doing this, like including
the db credentials IN the Database class construct method, but that
just seems out of place! I mean, sure, place the $link and db_select
stuff there, but not the credentials, right?
I'm hoping to stay as close to what I have up there as possible, like
just adding field scopes or something like that.
Any help is GREATLY appreciated.
Navigation:
[Reply to this message]
|