This is a discussion on Is this correct OO implementation within the PHP Language forums, part of the PHP Programming Forums category; I am designing a site and I was just wondering if the folowing class I designed, which is fairly simple, ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I am designing a site and I was just wondering if the folowing class I
designed, which is fairly simple, should be used. <?php class DB { private $username; private $password; private $db; private $server; //contruct function __contruct(string $username,$password,$server,string $db){ $this->username = $username; $this->password = $password; $this->db = $db; $this->server = $server; } //connect static connect () { @mysql_connect($this->server,$this->username,$this->password) or die ("Could not establish a connection with the MySQL database.\n"); @mysql_select_db($this->db) or die ("Could not select the database" . $this->db . "\n"); } //getter methods function get_username(){ return $this->username; } function get_password (){ return $this->password; } function get_db(){ return $this->db; } function get_server (){ return $this->server; } //setter methods function set_username ($username){ $this->username = $username; } function set_password ($password){ $this->password = $password; } function set_db ($db){ $this->db = $db; } function set_server ($server){ $this->server = $server; } } ?> I figured if I wanted to change which database to use I could do it quickly with a setter method, check it with a getter method. Also, apply this to usernames and passwords and servers. Is this too much code for a simple task? |
|
|||
|
<?php
class sqlException extends Exception{ function __construct($exception){ parent::Exception($exception); } } class sqlConnect{ private $username; private $password; private $db; private $server; private $link; //contruct function __construct($username,$password,$server,$db){ $this->username = $username; $this->password = $password; $this->db = $db; $this->server = $server; $this->link = @mysql_connect($this->server,$this->username,$this->password) if(!$this->link){ throw new sqlException(mysql_error()); } if(!@mysql_select_db($this->db){ throw new sqlException(mysql_error()); } } //destruct function __destruct(){ mysql_close($this->link); } ?> Better? |
|
|||
|
<?php
class sqlException extends Exception{ function __construct($exception){ parent::Exception($exception); } } class sqlConnect{ private $username; private $password; private $db; private $server; private $link; //contruct function __construct($username,$password,$server,$db){ $this->username = $username; $this->password = $password; $this->db = $db; $this->server = $server; $this->link = @mysql_connect($this->server,$this->username,$this->password) if(!$this->link){ throw new sqlException(mysql_error()); } if(!@mysql_select_db($this->db){ throw new sqlException(mysql_error()); } } //destruct function __destruct(){ mysql_close($this->link); } ?> Better? |
|
|||
|
http://www.pastebin.com/132165
I am wondering if my throws are right and i can use mysql_error() there. |
|
|||
|
On 21 Dec 2004 18:12:33 -0800, Jibbs <ryan.fairchild@gmail.com> wrote:
> <?php > class sqlException extends Exception{ > function __construct($exception){ > parent::Exception($exception); > } > } parent::__construct(); > Better? Much better IMO. When looking for PHP5 docs I keep running into: http://www.phpvolcano.com/articles/eclipse/index.php which shows pretty nice DB model in PHP5. -- porneL |
|
|||
|
"Jibbs" <ryan.fairchild@gmail.com> wrote in message
news:1103681553.030771.196410@f14g2000cwb.googlegr oups.com... > <?php > class sqlException extends Exception{ > function __construct($exception){ > parent::Exception($exception); > } > } > > class sqlConnect{ > > private $username; > private $password; > private $db; > private $server; > private $link; > > > //contruct > function __construct($username,$password,$server,$db){ > $this->username = $username; > $this->password = $password; > $this->db = $db; > $this->server = $server; > $this->link = > @mysql_connect($this->server,$this->username,$this->password) > > if(!$this->link){ > throw new sqlException(mysql_error()); > } > > if(!@mysql_select_db($this->db){ > throw new sqlException(mysql_error()); > } > > } > > //destruct > function __destruct(){ > mysql_close($this->link); > } > > ?> > > Better? The proof is always in the pudding, so try writing a class that deals with the ODBC api and see if your code can be cleanly ported (I know it cannot, your constructor parameters for instance will not be applicable for an ODBC connection). Your design should/could use one of three techniques in order to initialise the connection. 1) embed the connection parameters into constants and set them inside the class file or a common configuration file. The constructor then just uses the parameters that are relevant to it's own implementation. 2) pass the parameters to the constructor in an array. 3) get the constructor to read a configuration file either in XML or ini format. Is there any value in keeping the connection parameters in the object once connection has been established? I can think of excellent reasons why the username/password should be discarded. Your initial design assumes that the user may wish to manually set the parameters before attempting connection. Your subsiquent decision to connect in the constructor invalidates this assumption. Therefore you might want to hand control back to the application, not the class. |
|
|||
|
I'll add a comment too--
You should store the connection reference as well, and pass it in to all your database functions. What would happen if you instantiated two or more objects of this class? If you're willing to accept that you can only create a single object, then it should also be a singleton. -- Paul Hanchett "CJ Llewellyn" <invalid@example.con> wrote in message news:cqcrbc$quv$1@slavica.ukpost.com... "Jibbs" <ryan.fairchild@gmail.com> wrote in message news:1103681553.030771.196410@f14g2000cwb.googlegr oups.com... > <?php > class sqlException extends Exception{ > function __construct($exception){ > parent::Exception($exception); > } > } > > class sqlConnect{ > > private $username; > private $password; > private $db; > private $server; > private $link; > > > //contruct > function __construct($username,$password,$server,$db){ > $this->username = $username; > $this->password = $password; > $this->db = $db; > $this->server = $server; > $this->link = > @mysql_connect($this->server,$this->username,$this->password) > > if(!$this->link){ > throw new sqlException(mysql_error()); > } > > if(!@mysql_select_db($this->db){ > throw new sqlException(mysql_error()); > } > > } > > //destruct > function __destruct(){ > mysql_close($this->link); > } > > ?> > > Better? The proof is always in the pudding, so try writing a class that deals with the ODBC api and see if your code can be cleanly ported (I know it cannot, your constructor parameters for instance will not be applicable for an ODBC connection). Your design should/could use one of three techniques in order to initialise the connection. 1) embed the connection parameters into constants and set them inside the class file or a common configuration file. The constructor then just uses the parameters that are relevant to it's own implementation. 2) pass the parameters to the constructor in an array. 3) get the constructor to read a configuration file either in XML or ini format. Is there any value in keeping the connection parameters in the object once connection has been established? I can think of excellent reasons why the username/password should be discarded. Your initial design assumes that the user may wish to manually set the parameters before attempting connection. Your subsiquent decision to connect in the constructor invalidates this assumption. Therefore you might want to hand control back to the application, not the class. |