I'm new to classes and oo. I was looking for a MySQL base class to get started, and I found Matthew Zaragoza's “Simple MySQL Class”.
These are the first lines:
define('SIMPLE_DB_SERVER', 'mysqlserver.net'); define('SIMPLE_DB_NAME', 'mydbname'); define('SIMPLE_DB_USERNAME', 'myusername'); define('SIMPLE_DB_PASSWORD', 'mypassword'); class ASimpleMySQLDB { public function __construct($server, $database, $username, $password){ $this->conn = mysql_connect($server, $username, $password); $this->db = mysql_select_db($database,$this->conn); } [...]
I wonder if there is a specific reason why constants are defined outside the class instead of using the constructor - an example:
public function __construct(){ $this->conn = mysql_connect('localhost', 'username', 'password'); $this->db = mysql_select_db('database',$this->conn); }
Or:
public function __construct($which_db){ if($which_db=='firstdb'){ $server='localhost'; $username='user1'; $password='pass1'; $database='db1'; }elseif($which_db=='otherdb'){ $server='localhost'; $username='user2'; $password='pass2'; $database='db2'; } $this->conn = mysql_connect($server, $username, $password); $this->db = mysql_select_db($database,$this->conn); }
Or using a switch or something else.
What is the difference between these two methods? What would you recommend? Thank you very much: -)
Giona source share