Is this correct OO implementation

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, ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-21-2004
Jibbs
 
Posts: n/a
Default Is this correct OO implementation

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?

Reply With Quote
  #2 (permalink)  
Old 12-21-2004
Jibbs
 
Posts: n/a
Default Re: Is this correct OO implementation

I just realized that it should be static function connect.
Sorry about the typo

Reply With Quote
  #3 (permalink)  
Old 12-21-2004
porneL
 
Posts: n/a
Default Re: Is this correct OO implementation


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
Reply With Quote
  #4 (permalink)  
Old 12-22-2004
Jibbs
 
Posts: n/a
Default Re: Is this correct OO implementation

<?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?

Reply With Quote
  #5 (permalink)  
Old 12-22-2004
Jibbs
 
Posts: n/a
Default Re: Is this correct OO implementation

<?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?

Reply With Quote
  #6 (permalink)  
Old 12-22-2004
Jibbs
 
Posts: n/a
Default Re: Is this correct OO implementation

http://www.pastebin.com/132165

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

Reply With Quote
  #7 (permalink)  
Old 12-22-2004
porneL
 
Posts: n/a
Default Re: Is this correct OO implementation

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
Reply With Quote
  #8 (permalink)  
Old 12-22-2004
Jibbs
 
Posts: n/a
Default Re: Is this correct OO implementation

Hey, dude thanks alot for you help!

Reply With Quote
  #9 (permalink)  
Old 12-22-2004
CJ Llewellyn
 
Posts: n/a
Default Re: Is this correct OO implementation

"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.






Reply With Quote
  #10 (permalink)  
Old 12-30-2004
Paul Hanchett
 
Posts: n/a
Default Re: Is this correct OO implementation

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.






Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:59 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0