April 30th, 2008
PHP Object Oriented Programming
Multiplicity
My data extraction class has a flaw. My example class groups the HTML output into an array. This array, being associative, can hold only one row of data from the same database table. Entering a second row of data (from the same database table as the first row) into the array overwrites the original row. Additionally, entering data from a second (different) table that contains one or more columns with identical names as in the first table will override the data from the first table. I could have prevented this with better programming, but I want to show you an example of how I can fix this without having to modify code. This is where multiplicity comes in handy.
Multiplicity is the practice of using multiple instances of the same class. In this case it increases productivity because there is no need to do extra programming. For example, suppose that you need to say hello to two users instead of one. The siteaimproved.php code includes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php require_once("dataextractionclass.php"); $data1 = new DataExtraction(); $data2 = new DataExtraction(); $data1->deWhereStatement("WHERE id = '2'") $data1->deExtractDataHtml("users_table", "id", "username", "email"); $data2->deWhereStatement("WHERE id = '3'") $data2->deExtractDataHtml("users_table", "id", "username", "email"); echo("Hello, ".$data1->deGetData("username")."! "); echo("Hello, ".$data2->deGetData("username")."!"); ?> |
Assuming “TestUser” has an ID of 2 and “AnotherUser” has an ID of 3 in the users_table table, that will output the text Hello, TestUser! Hello, AnotherUser!
As you can see, by instantiating a second instance of the class, you can now have multiple rows of data from the same database table. The second instance of the class is a separate object and exists independently of the first. Keep in mind that this might start to hog system resources if you need to call thousand of rows that need to instantiate thousands of objects. You probably wouldn’t use this class in such a way, though. I’m simply trying to illustrate a point.
Versatility
Suppose that a new project comes along. It uses Flash and needs to display one row of information from a database. You could create an entirely new set of code and spend a ridiculous amount of time reprogramming old functions, or you could modify the original class. Here’s a new function to add into dataextractionclass.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | // Usage deExtractDataFlash("db_table", "field1", "field2", "field3") function deExtractDataFlash($table) { $args = func_get_args(); foreach($args as $k=>$v) { if($k != 0) { $fields .= trim($v).", "; } } //takes off the trailing comma and whitespace $fields = substr($fields, 0, (strlen($fields) - 2)); $query = "SELECT $fields FROM $table $this->whereStatement"; //Ex: WHERE waterId='1' $this->dbDataExtraction->dbConnect_Db(); //calls another class within this class $result = mysql_query($query) or die("MySQL Query Error: ".mysql_error()); while($row = mysql_fetch_array($result)) { foreach($args as $k=>$v) { if($k != 0) { //outputs in flash variable format echo("&".$v."=".$row[$k-1]); } } } } |
You can now call this function from the original dataextractionclass.php class and use it in conjunction with Flash movie files. All that you have to do is insert the new function into the original class file. Sometimes you will find the need to keep your original class intact without changes. In that case, use the keyword extends to add to an existing class without having to modify its file.
For example, the original class name is DataExtraction. Use extends to avoid changing the original class file like so:
1 2 3 4 5 6 7 8 9 10 | <?php class FlashExtraction extends DataExtraction { /* Flash Extraction Function Here */ } ?> |
Class extensions, unlike class functions, can go within separate files. In order to take advantage of this technique, however, keep in mind that you must define or include the original class within your PHP page before the class extension.
Pages : 1 2 3 4

