Forum Moderators: coopster

Message Too Old, No Replies

OO PHP5 - Re-Inventing the wheel

Creating Your Own MySQL Class

         

RDWest2005

5:16 pm on Aug 12, 2007 (gmt 0)

10+ Year Member



Hello guys,
I'm trying to move on to OOPHP and I'm having a heck of a time understanding the process. I've got several windows open with howto's etc.

I think it is best for me to start out and create my own classes so maybe the oo will just click in my brain.

I've come up with this for mysql connect and select db...

class mySQL {

public $dbHost = "";
public $dbUser = "";
public $dbPass = "";
public $dbName = "";
public $dbLink = "";
public $database = "";

function mySQL() {
$this->dbHost = 'localhost';
$this->dbUser = 'root';
$this->dbPass = '';
$this->dbName = 'testdb';
}

function myConnect() {
$dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass);
return $dbLink;

}
function myDatabase() {
$database = mysql_select_db($this->dbName);
return $database;
}

}

And then I call it in my script...
$myTest = new mySQL;
$link = $myTest->myConnect();
$database = $myTest->myDatabase();

Am I going about this right?
Thanks,
~RD

darrenG

6:51 pm on Aug 12, 2007 (gmt 0)

10+ Year Member



Yes, this is a correct implementation of a class.

I would suggest that as you build the class up, the constructor function handles the database connection and selection, purely for simplicity of coding scripts that implement the mysql class. :)

functions myConnect and myDatabase are missing some '$this->'s though..

RDWest2005

1:08 am on Aug 13, 2007 (gmt 0)

10+ Year Member



Well, like I said. I'm having a hard time with something maybe so simple. I've never coded in oo before.

So I guess you mean...
function myConnect() {
$this->dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass);
return $this->dbLink;

}
function myDatabase() {
$this->database = mysql_select_db($this->dbName);
return $this->database;
}

I'm I on the right trach now?

I'm so use to creating varibles...
Thanks
RD

RDWest2005

1:34 am on Aug 13, 2007 (gmt 0)

10+ Year Member



I'm working my way through here I think.
I'm doing if() debug "text" to test each one...

by defining debug as empty via = ""; and in my script going with .= and output is success etc...

class mySQL {

public $dbHost = "";
public $dbUser = "";
public $dbPass = "";
public $dbName = "";
public $dbLink = "";
public $database = "";
public $dbclose;

function mySQL() {
$this->dbHost = 'localhost';
$this->dbUser = 'root';
$this->dbPass = '';
$this->dbName = 'testdb';
}

function myConnect() {
$this->dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass);
return $this->dbLink;

}
function myDatabase() {
$this->database = mysql_select_db($this->dbName);
return $this->database;
}
function myClose(){
$this->dbclose = mysql_close($this->dbLink);
}

}

and at end of my script I add...

$myTest->myClose();

any suggestions?
am I on the right track?

One more question...

Can I access something inside the fuctions?
I mean by creating new (class)
I'm making the object $var->fuction and whats in fuction runs...

Is all accessed like this or can I access $var->func->var2?

Thanks,
RD

PS: I've never used classed because I've seen how slow CMS's can get etc...
Also by testing speeds of DB class handlers etc I've always found the few simple lines are twice as fast then having to go through thousands of lines of code etc...

So I think each applications needs to be unique and stripped of every line possible...

Thanks

[edited by: RDWest2005 at 1:38 am (utc) on Aug. 13, 2007]