You are here: Re: PHP Database Class Instantiation « PHP Programming Language « IT news, forums, messages
Re: PHP Database Class Instantiation

Posted by Dikkie Dik on 09/02/06 22:32

> 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);
> }

Why use globals here? If you define the database class with:

class Database
{private $link;
function __construct($host, $user, $password, $databasename)
{$this->link = mysql_connect($host, $user, $password);
mysql_select_db($databasename,$this->link);
}

etc. I personally would check the link after the connect.

> function getData() {
> $query = ...
> $result = ...
> while (...) {
> $this->data['name'] = $row['name'];
> ...
> }
> }

I don't understand this one. What does it do? should it return
something? Is the query hardcoded in this function?

>
> }
>
> class Stuff {
> function __construct() {
> $database = new Database <---- this does not seem to work
> }

Does it not work because of the syntax error? If so, switch on your
notices and warnings.
In my example, it should read:
$database = new Database(DB_HOST, DB_USER, DB_PASS, DB_NAME);

This way, you can pass other databases for testing or other users for
different roles (read / write / restructure).

> function getData() {
> $database->getData();
> $this->data = $database->data;
> }
This is by far easier to understand and maintain if you just have your
function use the return keyword. he getData method is really odd. It
does not accept input, and does not generate output either. Nonetheless,
I expect it to do some specialized job that generates output. Why isn't
it passed a query and returns the results?

>
> }
>
> - 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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация