[nycphp-talk] Should PHP5 be used for this?
Tom Sartain
tomsartain at gmail.com
Tue Jul 17 13:42:37 EDT 2007
>
> when I see a task, and I think of how to do it with objects, I see more
> code and more obstacles than just placing it in the file (not even
> necessarily a function), and more work, for nice, but not essential
> benefits.
Think of it this way: All computer programming can be reduced to procedural
code. Run line 1, followed by line 2, followed by line 3 and so on. This
will work. It's a hell to code the first time through, and beyond hell to
maintain. It's just a breeding ground for bugs.
So we create functions. Repeated code gets thrown in a function to be
re-used. If there is a bug found, we fix it once, and it's fixed for every
time that function is called. Nice. But when we start manipulating
variables, we run into a problem. We end up passing the same variables to
all these functions. Sometime we want to edit those variables. We could just
pass by reference, but that can get messy. And typically we find that the
same set of variables are used over and over.
So we create objects. Now we have the wonders of encapsulation. Everything
that has to do with one specific thing is grouped together. For example,
look at eBay. When you do a search on eBay, you (hopefully) get a screen
full of search results. Each of those could be an instance of a bid object.
Then each one is calling a special method (function) of the object to
display search results.
$searchResults; // array containing the ids of the various auctions
foreach ($searchResults as $auctionID)
{
$auction = new auction($auctionID);
$auction->showSearchResult();
}
And there you go, every auction result is displaying a similarly formatted
result to the user. You can make a change once in the object and have it
reflect anywhere it's referenced. Plus, you don't have to worry about
variable wrangling, since all the variables associated with each auction are
encapsulated inside the object and can't be changed by accident. You know
exactly which set of data you are working with and changing.
For me, it was sort of a paradigm shift to OOP. One day I was thinking
"What's the point of creating all these objects when I can do it all with
functions and arrays?" and then something clicked and I realized "Objects
make life so much easier and clearer. I think this one just baked me
cookies. I like objects."
Okay, maybe the last bit didn't occur, but it could have. A cookie factory
class.... mmm
On 7/17/07, Brian O'Connor <gatzby3jr at gmail.com> wrote:
>
> Hope my last email was received more as playful than annoying. I
> > figured you had done what I've done too many times in the past and just
> > sent a message before it was ready to go out. Happens to all of us.
> >
>
> Yes, I took it as a playful response to my mistake, I laughed :)
>
> Thanks to everyone who has responded so far, I was thinking of looking at
> CakePHP already so this is just more reason to do so.
>
> I think more of my problem is when I see potential objects, I see more
> code, and more work to accomplish the same goal.
>
> I think when I said PHP5 in my previous post, I really meant OOP. I can
> switch to PHP5 without using OOP, which isn't my problem. It's more
> migrating toward the OOP approach to web development. My problem is as of
> now, when I see a task, and I think of how to do it with objects, I see more
> code and more obstacles than just placing it in the file (not even
> necessarily a function), and more work, for nice, but not essential
> benefits.
>
> Someone I develop with created a good User class that I've been looking
> at, and I feel that's a good example of something to use an object for.
> Other examples that I could look into would be very helpful.
>
> On 7/17/07, Allen Shaw <ashaw at polymerdb.org> wrote:
> >
> > Brian O'Connor wrote:
> > > Here's my problem : I don't see how using php5 and oop would really
> > > benefit me in building websites. I understand objects, code
> > reusability
> > > and all that. So I'm going to put the areas of my site here, and see
> > > how you guys would build objects for them.
> > >
> > > I have these sections:
> > > News
> > > Portfolio
> > > Code (samples of code that I've written, for resume-type purposes)
> > > Tutorials
> > > Contact
> > > About Me
> > >
> > > At first glance, I thought to object-ify the portfolio section, where
> > > each "project" would be an object. But other than that, I don't see
> > > anywhere on the site to use objects, and I could easily do that
> > without
> > > objects.
> >
> > Hi Brian,
> >
> > Hope my last email was received more as playful than annoying. I
> > figured you had done what I've done too many times in the past and just
> > sent a message before it was ready to go out. Happens to all of us.
> >
> > You're getting good advice from others already on the OOP questions.
> > It's time to move on from PHP4, and rebuilding a site sounds like the
> > perfect opportunity. A personal site can also be a great chance to
> > experiment with new concepts.
> >
> > - Allen
> >
> > --
> > Allen Shaw
> > Polymer (http://polymerdb.org)
> > slidePresenter (http://slides.sourceforge.net )
> > _______________________________________________
> > New York PHP Community Talk Mailing List
> > http://lists.nyphp.org/mailman/listinfo/talk
> >
> > NYPHPCon 2006 Presentations Online
> > http://www.nyphpcon.com
> >
> > Show Your Participation in New York PHP
> > http://www.nyphp.org/show_participation.php
> >
>
>
>
> --
> Brian O'Connor
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
>
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20070717/a83e6394/attachment.html>
More information about the talk
mailing list