Simple Question

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Simple Question

Post by s.dot »

Albeit I always think my questions are simple, they always tend to get drug out into long discussions. =/ Anyhoot, is it "acceptable" to hardcode a username and password into a database class? So I could just call a connect function. Perhaps something like this:

Code: Select all

class db
{
   var $host = 'localhost';
   var $user = 'username';
   var $pass = 'password';

   function connect()
   {
      mysql_connect($this->host,$this->user,$this->pass);
   }

}
So on a script I can just call

Code: Select all

require 'db.class.php';
$db = new db();
$db->connect();
Forgive me if this is how it's "usually" done, as I don't much look at others code. To me this seems logical because this is for one project that only I will ever see the code to. However, I don't want to start a bad practice.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

This is also how i personally do it as i have never had the need arise where i would need to connect as a different user

perfectly acceptable in my opinion
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Re: Simple Question

Post by Oren »

If you do it like that, then why not doing it like this?

Code: Select all

class db
{
  function connect()
   {
      mysql_connect('localhost', 'username', 'password');
   }

}
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

i do it scottay's way purely because i prefer the seperation of potentially 'changing' configuration options, that way i do not need to even touch the methods, only the classes configuration vars
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Simple Question

Post by s.dot »

Oren wrote:If you do it like that, then why not doing it like this?

Code: Select all

class db
{
  function connect()
   {
      mysql_connect('localhost', 'username', 'password');
   }

}
Also, should the need arise to perhaps have a certain script connect to a different db (never happened to me before, just something that MIGHT occur), I could change the settings by doing:

Code: Select all

$db = new db();
$db->user = 'some other user';
$db->pass = 'xxxxxx';
Right?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

None of my details are ever kept within the class. Simply because I use the same class on many applications.

I use my machine and my database for developing applications for clients, with my classes, so the login details will differ.

All configurable items are kept in a configurations file or medium (table for example).

Ease of maintenance..
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

I have a mySQL wrapper class, which i use basically three methods from it:

MySQL::getAll( $sql );
MySQL::execSQL( $sql );
MySQL::nextObject();

The execSQL method is the only place in the entire code with the mysql_query function being used. Here I check for an open mysql resource, and if its not available, then I call a static method in that class to connect. This way, you never have to actually connect to the database explicitly, it handles it for you.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Jenk wrote:None of my details are ever kept within the class. Simply because I use the same class on many applications.

I use my machine and my database for developing applications for clients, with my classes, so the login details will differ.

All configurable items are kept in a configurations file or medium (table for example).

Ease of maintenance..
But.. all you would have to do is simply change the login credentials in the class for each project. Seems like storing them somewhere else would take away from the ease of maitenance?

Sorry, I'm just a bit confused :P

Also, it seems like while I'm writing this I'm just giving different names to the php functions. The reason I wanted to write this is to cut down on always typing out queries and other db functions the long way, to save some typing.. and to experience dealing with objects.

So I suppose it is a "wrapper" if that's what it's called. Can someone tell me the purpose of such a script, other than to cut down on coding?

Perhaps it is to get a solid class and reduce the chance of coding errors? To have something to transport from project to project?

Seems to me if I start getting used to a class that I write (such as this db class), I will start forgetting the real php functions!

Here's what I got so far:

Code: Select all

<?php

//temp
ini_set("display_errors","On");
error_reporting(E_ALL);

/*
* MySQL Database Class
*/

class db
{
	
	var $host		= 'localhost';
	var $user		= 'user';
	var $pass		= 'pass';
	var $db_name	= 'db_name';
	var $resource;
	
	//connect to mysql server
	function connect()
	{
		if($this->resource = mysql_connect($this->host,$this->user,$this->pass))
		{
			return true;
		} else
		{
			return false;
		}
	}
	
	//select database
	function select_db()
	{
		return mysql_select_db($this->db_name,$this->resource);
	}
	
	//query the database
	function query($sql)
	{
		return mysql_query($sql);
	}
	
	//fetch array from result set, defaults to MYSQL_ASSOC
	function fetch($result,$type=MYSQL_ASSOC)
	{
		return mysql_fetch_array($result,$type);
	}
	
	//frees result set from mysql memory
	function free_result($result)
	{
		mysql_free_result($result);
	}
	
}

?>
[edit] I suppose I went from a simple question, to... something else, didn't I? :oops:
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Do you use a framework with a service locator?

Do you remember to open up your mysql class file everytime you create a new application?

Ease of maintenance:

Code: Select all

<?php

$db = new MySQLClass('host', 'user', 'pass');

?>
Not ease of maintenance:

Open folders > Open Class file > Edit login details >

Code: Select all

<?php

$db = new MySQLClass();

?>
Then there is also the situation of what if the client changes database name/password? They don't want to have to manually edit the source files, that's why the paid you in the first place. They just want an easy to use config handler such as a webpage or even an ini file.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Well if it works...it's acceptable ;)

By personal choice...I wouldn't do that...

It's bad practice and poor use of OOP IMHO...

You'd be better passing an Ado object (initialized somewhere else) to your class and using the object instead of calling mysql_* directly or hard coding user/pass details inside a class...

Just a personal choice though...either is perfectly technically valid, just the latter is better suited for project longevity... :)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Heh, but then it would be a choice of whether to edit the class, or edit the object instantiation. Either way would still need editing. Even a webpage or ini file... still editing. I get the point though. It's pretty much a matter of preference.

What about this?
scottayy wrote:So I suppose it is a "wrapper" if that's what it's called. Can someone tell me the purpose of such a script, other than to cut down on coding?

Perhaps it is to get a solid class and reduce the chance of coding errors? To have something to transport from project to project?

Seems to me if I start getting used to a class that I write (such as this db class), I will start forgetting the real php functions!
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

But with a webpage/configfile/table you'd only need to edit the one thing when installing the app or when the system changes etc, over potentially editing every class file that has properties set like that :)
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

What's wrong with some constant or something set in a config file?

IMHO, you shouldn't have to recode a lib everytime you start a project or change database machines.
Post Reply