RE: [PHP] OOP in web development

From: Date: Wed, 14 Feb 2001 08:51:20 +0000
Subject: RE: [PHP] OOP in web development
Groups: php.general 
Request: Send a blank email to php-general+get-39601@lists.php.net to get a copy of this message
---------------------------------------------------------------------------- ----------------- Disclaimer: The information contained in this email is intended only for the use of the person(s) to whom it is addressed and may be confidential or contain legally privileged information. If you are not the intended recipient you are hereby notified that any perusal, use, distribution, copying or disclosure is strictly prohibited. If you have received this email in error please immediately advise us by return email at postmaster@normandy.com.au and delete the email document without making a copy. ---------------------------------------------------------------------------- ----------------- I have to agree that OOP is much better in abstracting than user defined functions... why? To return some data and display in a HTML table it i do: $db = new DB; $db->query("SELECT foo from bar"); $t = new Table; echo $t->create($db->data); Pretty easy. Now i could have all these have be functions $data = query($connection_details,"SELECT foo from bar"); echo create_table($data); But.... What if i wanted a special connection specified as myDB that was consistant through out my site? Or what if my table creation function has over 30 different options setable (colours, fonts, spacing by cells, columns, rows and table)? This becomes difficult with pure functions as you end up with echo create_table($data,"nowrap","black",null,"white",null,null,null,"Tahoma",12, null,null,null,array(array("bold"))); instead of $t = new Table; $t->options = "nowrap"; $t->heading["bgcolor"] = "black"; $t->heading["fontcolor"] = "white"; $t->global["font"] = "tahoma"; $t->global["fontsize"] = 12; $t->column["total"]["format"] = "bold"; echo $t->create($db->data); // Create another table with similiar properties, but different data echo $t->create($mydb->data); What if wanted to make my table definition reusable (you would have to make sure all these class's are included like your functions) class myTable extends Table { var $options = "nowrap"; var $heading["bgcolor"] = "black"; var $heading["fontcolor"] = "white"; var $global["font"] = "tahoma"; var $global["fontsize"] = 12; var $column["total"]["format"] = "bold"; } Then from then on you could just call $t = new myTable; echo $t->create($db->data); Now if change my definition of what a myTable should look like i change it once, not in everyfile as you might with functions. (You could however define a new function called create_myTable that does something similiar). The last benefit is that with phpdoc and similiar tools class's can be autodocumented which is very nice. Readability, maintainability and reusability are much higher with OOP. Mark Nold markn@enspace.com <mailto:markn@enspace.com> Senior Consultant   Change is inevitable, except from vending machines. -----Original Message----- From: Jeff Warrington [mailto:jaydub@bossanova.com] Sent: Wednesday, February 14, 2001 4:42 AM To: php-general@lists.php.net Subject: Re: [PHP] OOP in web development In article <5.0.2.1.0.20010213093835.00b339e0@mail.wizaerd.com>, "Joe Sheble (Wizaerd)" <JustForMailingLists@wizaerd.com> wrote: depending on the nature of what you are doing, one of the things that i like about using classes is the ability to group functionality under a larger structure, the class. Instead of having a series of disconnected functions, i can instead write them as class methods. another feature that is a basic feature of oop stuff is the idea of inheritence. So you can have a base class (CAR) that defines behaviour common to all instanaces of that class (var wheels, var door, method car start, method car turn signal on, etc...) Then you can subclass the CAR class to define specific behaviour for a particular car model (SPORTSCAR - var turbo, method car turbo on, etc..). again, depending on what you are doing, this can be very beneficial. I suggest you start small and also take a look at other people's code. You will find alot of classes out there that make alot of sense and can give you ideas of new ways of doing things.... Jeff > I've been using PHP for over a year now and have been successfully > running three different websites developed with PHP, but I've never > done anything with classes or objects. Even when returning data from a > mySQL database, I use mysql_fetch_array() instead of > mysql_fetch_object(). > > What am I missing by not using objects and classes, other than > reusability? What are the real benefits to using OOPs in PHP? > >

« previous php.general (#39601) next »