473,394 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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?

Jul 17 '05 #1
9 1982
I just realized that it should be static function connect.
Sorry about the typo

Jul 17 '05 #2

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
<?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
<?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
http://www.pastebin.com/132165

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

Jul 17 '05 #6
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
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
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
.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Richard Golebiowski | last post by:
I have been trying to figure this out for quite some time and cannot find any examples in VB.Net or in VB that work correctly. I am working on an application where I want the user to be able to...
24
by: Alf P. Steinbach | last post by:
The eighth chapter (chapter 2.1) of my attempted Correct C++ tutorial is now available, although for now only in Word format -- comments welcome! Use the free & system-independent Open Office...
53
by: Alf P. Steinbach | last post by:
So, I got the itch to write something more... I apologize for not doing more on the attempted "Correct C++ Tutorial" earlier, but there were reasons. This is an UNFINISHED and RAW document,...
28
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
27
by: Yang Lee | last post by:
Hi, If I write char *p="hello world"; is this correct in C or do i have to assign memory block and then strcpy the string to pointer. If not correct in C , is it allowable in C++ , i...
6
by: Rob Thorpe | last post by:
Given the code:- r = sscanf (s, "%lf", x); What is the correct output if the string s is simply "-" ? If "-" is considered the beginning of a number, that has been cut-short then the...
3
by: Jarmo Muukka | last post by:
Hi all! I have a small problem I cannot find a good solution. The problem is destructing global (static) variables in correct order. I try to mimic it in here. // file1.cpp contains...
2
by: Mike | last post by:
Hello NG, i am just learning various Design Patterns and now i am not sure, if this design is correct (Builder) or if i should use an other pattern. I have various classes (here ChildA and...
9
by: Maximus | last post by:
wchar_t ch2 = L'\u27BA'; wchar_t ch = '\u27BA'; On Visual C++ 2003 SP1, no compiler warnings are given at all. However, ch != ch2. Which one is correct? ---
13
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
If you have: #define errno retrieve_errno_func() #define SUBSYSTEM_INCLUDE(subsystem, file) <subsystem/include/file> then what should happen when you do:
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.