|
Posted by Slant on 09/30/85 11:57
Fantastic reply! Thanks for taking the time. I shall now attempt to
update my situation in order to hopefully clear up any confusion.
I figured out how to get the Stuff class to grab data from the Database
class. Here is an updated version of my code:
class Database {
function __construct() {
// Save the database credentials
$db['host'] = "localhost";
$db['user'] = "root";
$db['pass'] = "...";
$db['name'] = "tbl";
// Initialize the database connection
$link = mysql_connect($db['host'],$db['user'],$db['pass']);
mysql_select_db($db['name'],$link);
}
function getLinks() {
$query = "SELECT * FROM links";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$this->links[$row['id']]['label'] = $row['label'];
$this->links[$row['id']]['url'] = $row['url'];
$this->links[$row['id']]['location'] = $row['location'];
}
}
}
class Link {
function __construct() {
$this->database = new Database;
}
function getLinks() {
$this->database->getLinks();
$this->links = $this->database->links;
}
}
This is exactly what I'm using now, in fact. I've done away with the
arbitrary naming from before. So, the Link class instantiates a
Database object, then passes a request to it. The database object then
returns a variable which is then accessed by the index.php file and
displayed there in a foreach statement.
Your questions:
Why use a global? I'm not really even sure how to NOT use a global.
I'm assuming you're referring to the scope, right? This actually is
why I'm still on this thread. My question today ties directly into
this: How can I define some constants in a config.php file or something
of the like, then access those constants from within the Database class
directly without HAVING to pass the variables every time I instantiate
a Database object? As you can see now, I am actually declairing the
variables INSIDE of the database class... not my cup of tea.
What does the Stuff class do?
It's just a go between so the actually layout can access the database
through another layer. It makes sense in the grander scheme of my
application, just not here. I know it look unneccessary.
My question:
Was just asked in the "Why use a global?" answer.
Thanks as always for your help!!
Navigation:
[Reply to this message]
|