Classes are great for code reusability, I heavily use a MySQL object or class to make all my conections to a mysql database now and I have included methods for Transactions.
Its a long one but its the class I use the most and is a great example of what they are good for. <? class mysqldb { //so here define the name of the class. //set up the object, these are the variables that are accessible by each instance you create // of an object. var $host; var $db; var $dbuser; var $dbpassword; var $sql; var $numberrows; var $dbopenstatus; var $dbconnection; var $qry; var $result; var $TransactionSwitch; /* Use these functions to get and set the values of this object's variables. This is good OO practice, as it means that datatype checking can be completed and errors raised accordingly. */ // Property Get & Set these methods are used set class vars and to retrieve them. function gethost() { return $this->dbhost; } function sethost($req_host) { $this->dbhost = $req_host; } function getdb() { return $this->db; } function setdb($req_db) { $this->db = $req_db; } function getdbuser() { return $this->dbuser; } function setdbuser($req_user) { $this->dbuser = $req_user; } function getdbpassword() { return $this->dbpassword; } function setdbpassword($req_password) { $this->dbpassword = $req_password; } function getsql() { return $this->sql; } function setsql($req_sql) { $this->sql = $req_sql; } function getnumberrows() { return $this->numberrows; } function setnumberrows($req_numberresults) { $this->numberesults = $req_numberresults; } function setdbconnection($req_dbconnection) { $this->dbconnection = $req_connection; } function getdbconnection() { return $this->dbconnection; } function setTransactionSwitch($switch) { $this->TransactionSwitch = $switch; } function getTransactionSwitch() { return $this->TransactionSwitch; } /* This is the constructor for the object. In this case I have set the initial values of a number of the object properties to those values declared in the global constants.inc. By doing this, I only need to change the values of these properties for specific operations, which we will not need to do throughout this example */ function mysqldb() { global $HOST, $DB, $WEBUSER, $WEBPASSWORD; global $TRUE, $FALSE; $this->sethost($HOST); $this->setdb($DB); $this->setdbuser($WEBUSER); $this->setdbpassword($WEBPASSWORD); $this->setdbconnection($FALSE); } /* These are the methods for the object. They provide for opening a connection to the database, closing a connection and executing a SELECT query. Of course, these can be expanded upon to allow for INSERT's, UPDATE's and DELETE's etc... */ function opendbconnection() { global $TRUE, $FALSE; $this->dbconnection = mysql_connect("$this->dbhost", "$this->dbuser", "$this->dbpassword"); if ($this->dbconnection == $TRUE) { $this->db = mysql_select_db("$this->db"); $this->setdbconnection($TRUE); } else { $this->setdbconnection($FALSE); return false; } return true; } function closedbconnection() { if ($this->dbconnection = $TRUE) { mysql_close($this->dbconnection); } } function begin( ) { if ($this->dbconnection == $FALSE) { $this->opendbconnection(); } $this->setsql("BEGIN"); $this->qry = mysql_query($this->sql); if (!$this->qry) { return false; } else { return true; } } function rollback( ) { if ($this->dbconnection == $FALSE) { $this->opendbconnection(); } $this->setsql("ROLLBACK"); $this->qry = mysql_query($this->sql); if (!$this->qry) { return false; } else { return true; } } function commit( ) { if ($this->dbconnection == $FALSE) { $this->opendbconnection(); } $this->setsql("COMMIT"); $this->qry = mysql_query($this->sql); if (!$this->qry) { return false; } else { return true; } } function selectquery() { global $TRUE, $FALSE; if ($this->dbconnection == $FALSE) { $this->opendbconnection(); } $this->qry = mysql_query($this->sql); if (!$this->qry) { return false; } else { $this->numberrows = mysql_num_rows($this->qry); if ($this->numberrows > 0) { for($x = 0; $x < $this->numberrows; $x++) { $this->result[$x] = mysql_fetch_row($this->qry); } } else { // echo("[Error:] Retrieving data"); return false; } return true; } } function insertquery() { global $TRUE, $FALSE; if ($this->dbconnection == $FALSE) { $this->opendbconnection(); } $this->qry = mysql_query($this->sql); if (!$this->qry) { return false; } else { return true; } } function deletequery() { global $TRUE, $FALSE; if ($this->dbconnection == $FALSE) { $this->opendbconnection(); } $this->qry = mysql_query($this->sql); if (!$this->qry) { return false; } else { return true; } } function updatequery() { global $TRUE, $FALSE; if ($this->dbconnection == $FALSE) { $this->opendbconnection(); } $this->qry = mysql_query($this->sql); if (!$this->qry) { return false; } else { return true; } } } ?> And now in any .php file you include this .obj file and use it as follows. $db1 = new mysqldb(); // create a new instance of the mysql object. // You can create as many as you want and the great thing is // that each instances vars will contain there own data. $sql = "Insert into ..."; $db1->setTransactionSwitch("true"); $db1->begin(); //this will begin a transaction (InnoDb or BDB tables are required for this) $db1->setsql($sql); //set the objects $sql variable. if (!$db1->insertquery()) //now call the method that does all the work so $db1->setTransactionSwitch("false"); //you don't have to recode it. If it fails roolback. if($db1->getTransactionSwitch()=="false");{ $db1->rollback(); }else{ $db1->commit(); } Classes allow you to have multple instances of an object containing seperate info, makes it easier to keep track of. Hope this helps, Steve. -----Original Message----- From: Chris Crane [mailto:[EMAIL PROTECTED]] Sent: Tuesday, July 16, 2002 11:07 AM To: [EMAIL PROTECTED] Subject: Re: [PHP] Classes vs. Functions It helps a little bit, thank you. Could you provide some code as to what a Class looks like. I am just trying to understand it better and if I see it, it might help. "Jay Blanchard" <[EMAIL PROTECTED]> wrote in message 000401c22cd8$54ce9ce0$8102a8c0@niigziuo4ohhdt">news:000401c22cd8$54ce9ce0$8102a8c0@niigziuo4ohhdt... > [snip] > Could someone please explain the difference between classes and functions > and how to use a class. I write alot of PHP, but I never understood this at > all. I use an include statement in many of my pages and include a file with > a bunch of functions. For instance, I might have a function called stock(); > In the page I am using I include the file that has this function and I call > it like this: > > stock($Sym); > > I am wondering if I am doing it the wrong way. So I need to better > understand classes. What is one, and why would you use it? > [/snip] > > A class is the representation of an object, such as a person, place, or > thing. > A function is a group of commands that can be called for a specific purpose. > > function addNumbers() > > A function can be performed on an object, but an object (the logical > extension of class) cannot be performed on a function. Does that help? > > Jay > > "Cleverly disguised as a responsible adult" > > ************************************* > * Want to meet other PHP developers * > * in your area? Check out: * > * http://php.meetup.com/ * > * No developer is an island ... * > ************************************* > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php