By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,987 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,987 IT Pros & Developers. It's quick & easy.

Is this correct OO implementation

P: n/a
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?

Jul 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
I just realized that it should be static function connect.
Sorry about the typo

Jul 17 '05 #2

P: n/a

I think you should try to connect to the database in costructor and throw
exception if it fails.

database object without database connection is useless.
die() is not OO style.
--
porneL
Jul 17 '05 #3

P: n/a
<?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?

Jul 17 '05 #4

P: n/a
<?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?

Jul 17 '05 #5

P: n/a
http://www.pastebin.com/132165

I am wondering if my throws are right and i can use mysql_error() there.

Jul 17 '05 #6

P: n/a
On 21 Dec 2004 18:12:33 -0800, Jibbs <ry************@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
Jul 17 '05 #7

P: n/a
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" <in*****@example.con> wrote in message news:cq**********@slavica.ukpost.com...
"Jibbs" <ry************@gmail.com> wrote in message
news:11**********************@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.


Jul 17 '05 #8

P: n/a
Hey thanks for the tip. I have been reading on some more OO design and
I have a much better approach at this.

I have a huge problem right now, I am trying to implement Iterator
into my Mysql_Result class, but I can't find a good reference on how to
use it.

Jul 17 '05 #9

P: n/a
.oO(Jibbs)
I have a huge problem right now, I am trying to implement Iterator
into my Mysql_Result class, but I can't find a good reference on how to
use it.


Do you just have problems with the implementation of the required
methods or do you not even know where to start? The PHP manual gives at
least some basic informations about the iteration interfaces.

Object Iteration
http://www.php.net/manual/en/languag...iterations.php

Micha
Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.