RE: [PHP] OOP in web development

From: Date: Wed, 14 Feb 2001 18:16:28 +0000
Subject: RE: [PHP] OOP in web development
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-39701@lists.php.net to get a copy of this message
On Wed, 14 Feb 2001 19:21:20 +1030, Nold, Mark (mark.nold@normandy.com.au) wrote: >--------------------------------------------------------------------- >------- >----------------- >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,"Taho >ma",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); you could do something like: $parameters["options"] = "nowrap"; $parameters["heading"]["bgcolor"] = "black"; $parameters["heading"]["fontcolor"] = "white"; $parameters["global"]["font"] = "tahoma"; $parameters["global"]["fontsize"] = 12; $parameters["column"]["total"]["format"] = "bold"; echo create_table($data,$parameters); The difference is a matter of style. >// 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). this is nothing you can't do without OOP. >The last benefit is that with phpdoc and similiar tools class's can >be autodocumented which is very nice. Maybe so, I never used it. To me, the main reason to use OOP is when you're dealing with Java or something and native code is dealing with your objects. For instance laying out widgets on a screen might need that your widget extend Widget and have a draw() method. I've never had a situation with php where I thought doing things this way would make things easier. - Mark

« previous php.general (#39701) next »