|
Posted by Jerry Stuckle on 10/18/79 11:57
Slant wrote:
> Jerry Stuckle wrote:
>
>>Slant wrote:
>>
>>>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!!
>>>
>>
>>PMJI,
>>
>>Generally I pass the values to the database class. The main reason is
>>for security; a second is versatility.
>>
>>For instance - I might have one user for almost everyone who can read
>>the database but not update it (or at least certain tables) - or at
>>least only insert/update. But an admin user would be able to perform
>>additional operations.
>>
>>That way the "public" user would have limited privileges, while admin
>>would have full privileges. It helps in case for some reason the
>>password gets out (i.e. screwed up Apache configuration) and/or someone
>>tries a SQL injection attack that I didn't catch.
>>
>>
> Alright those are some darned good reasons. Thanks for taking the time
> to explain those. So how to you pass the variables? Just through the
> class instantiation from within the other class? Or do you do it from
> variables you actually DECLAIR in the calling class? I'm assuming the
> former from your statement. Thus might bring me to another question.
> Do you have multiple sets of user access credentials, one for each user
> of which you spoke earlier? Thus you could access these credentials
> anytime you'd like. Or how does that work?
>
> Thanks again!
>
>
(Top posting fixed)
Typically I'll pass them in the constructor for the object or the open()
method. If they're passed in the constructor, I open the connection
immediately. Otherwise I open it in the open() method.
Then the database class object would be passed as a parameter to the
links object. It allows for the same database object to be used by
several other objects. I wouldn't create it in the links class.
Also, your getLinks() method should be part of the Link class. The
database class should have things related to the entire database itself,
not a specific table. It makes it more reusable.
Typically my database class will only have methods such as the
constructor, open, close, etc. If you want to make it database
independent, you could even add things like query(), etc. to it, and
call mysql_query() from there. But if you don't care about that part,
just call mysql_query() from your Link class.
So you might be something like:
$db = new Database($host, $user, $pass);
$link = new Link($db);
$links = link->getLinks();
And yes, I have different users with different privileges, depending on
the need at the time.
P.S. Please don't top post. Thanks.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|