From: Andrew Brookins on
On Tue, Jan 19, 2010 at 7:11 AM, Ben Stones <b3ns93(a)googlemail.com> wrote:
> Hi,
>
> I've been learning about object oriented programming for the past few weeks
> and I've understood it pretty well, but I have one question. Usually with
> PHP scripts I make, all the functionality for a specific page is in the
> actual PHP file, and I'd use PHP functions in a separate directory which
> would be included in whichever PHP file needs specific functions I have
> created. The functions would be for the specific things in my script, such
> as validation checks, functionality that will be used/repeated a lot
> throughout my script, etc. What I don't understand about OOP is what its
> primary purpose is for. Do I use OOP for all the functionality of my
> application, in separate directories, and include these specific class files
> and call the methods to complete specific functionality needed for whatever
> PHP file I'm working on, or is OOP used for specific functionality like I
> would with functions? Essentially what I'm asking is what is the primary
> purpose for OOP? Hope you understand.
>
> Thanks,
>

Hey, Ben,

The primary purpose of object-oriented programming is to make code
easier to maintain.

Typically moving to an OO approach means designing your scripts with
objects in mind from the ground up. You might find it helpful to
start fresh with a new project and try to write it all with classes
and methods. This can be a challenge, depending on how long you have
been programming without objects.

I recommend that you find some open-source, object-oriented scripts
similar to ones you have worked on in the past and read through the
code to see how it's done. An object-oriented framework like
CodeIgniter can help get you on the right track, though there are also
pitfalls with using frameworks. Watch out for over-complex frameworks
that will only confuse you.

Also, there are a lot of poorly-written and/or non-OO PHP scripts
floating around out there, so it might help to read OO code written in
Python, Ruby, or some other scripting language. I have found that in
general, the quality of publicly available work is higher in those two
languages than in PHP.

Andrew
From: Ashley Sheridan on
On Fri, 2010-03-12 at 06:49 -0800, Andrew Brookins wrote:

> On Tue, Jan 19, 2010 at 7:11 AM, Ben Stones <b3ns93(a)googlemail.com> wrote:
> > Hi,
> >
> > I've been learning about object oriented programming for the past few weeks
> > and I've understood it pretty well, but I have one question. Usually with
> > PHP scripts I make, all the functionality for a specific page is in the
> > actual PHP file, and I'd use PHP functions in a separate directory which
> > would be included in whichever PHP file needs specific functions I have
> > created. The functions would be for the specific things in my script, such
> > as validation checks, functionality that will be used/repeated a lot
> > throughout my script, etc. What I don't understand about OOP is what its
> > primary purpose is for. Do I use OOP for all the functionality of my
> > application, in separate directories, and include these specific class files
> > and call the methods to complete specific functionality needed for whatever
> > PHP file I'm working on, or is OOP used for specific functionality like I
> > would with functions? Essentially what I'm asking is what is the primary
> > purpose for OOP? Hope you understand.
> >
> > Thanks,
> >
>
> Hey, Ben,
>
> The primary purpose of object-oriented programming is to make code
> easier to maintain.
>
> Typically moving to an OO approach means designing your scripts with
> objects in mind from the ground up. You might find it helpful to
> start fresh with a new project and try to write it all with classes
> and methods. This can be a challenge, depending on how long you have
> been programming without objects.
>
> I recommend that you find some open-source, object-oriented scripts
> similar to ones you have worked on in the past and read through the
> code to see how it's done. An object-oriented framework like
> CodeIgniter can help get you on the right track, though there are also
> pitfalls with using frameworks. Watch out for over-complex frameworks
> that will only confuse you.
>
> Also, there are a lot of poorly-written and/or non-OO PHP scripts
> floating around out there, so it might help to read OO code written in
> Python, Ruby, or some other scripting language. I have found that in
> general, the quality of publicly available work is higher in those two
> languages than in PHP.
>
> Andrew
>


I'd just like to add my own thoughts on OOP.

Whereas with conventional procedural PHP you might put your functions
outside in a separate file to be called whenever you need them, going
the OOP route allows you to section out your functions into groups as it
were.

Imagine a company site selling products and support online. You might
have an object to deal with the logins, shopping cart and shopping
history, another object for dealing with the blog/forum where you
support is given, maybe another for pulling the general content of the
page together. This would allow you to only load in those bits you need
on each part of the site and re-use specific parts on other sites
easily.

Thanks,
Ash
http://www.ashleysheridan.co.uk


From: Rene Veerman on
I also agree OOP is not a fad. Its a step up from procedural/including.
And it's still evolving.

While PHP is able to do polymorphism perfectly without OOP/classes,
through "require($plugin/className); $varFunctionName ($p1, $p2,
etc);", My newsscraper works very well that way.
But if you want inheritance features it's best to cross into OOP land.

A MVC app (front scripts that forward functionality to many other
scripts) does not have to include dozens of scripts to get things
done. You can require_once() the functionality that you need within
switch($command) statements.
Bytecachers and compilers furher reduce this problem (to
insignificance as far as i'm concerned).

But in my 20+ yrs exp, OOP-ing everything in an app from the start
costs too much (wasted) design time in the first half of any large
project.
So i've reverted back to sticking to procedural programming for nearly
everything, except well walled-off problems that scream "OOP me".

For my own CMS i first had designed classes to handle the
businesslogic near the database level (a dossier class containing refs
to media-item classes for instance), but as my CMS was evolving i was
spending too much time re-and-re-designing those dossier and media
classes. The conflict between "a well written class" and "a class that
does what i need atm" kept popping up.
I found that using functions and arrays as return/command "objects" is
much nimbler.
I get things done quicker not OOP-ing.
But i bet some other programmers prefer OOP over procedural for the same reason.

I do use OOP-ed objects for several of my subsystems (adodb.sf.net is
my current favorite), and if i have a small app to share (especially
in javascript) then i put it in an object, for namespace cleanness.

OP: if you want to use OOP to it's max, read
http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612/ref=sr_1_1?ie=UTF8&s=books&qid=1268432214&sr=8-1

On Tue, Jan 19, 2010 at 7:12 PM, Robert Cummings <robert(a)interjinn.com> wrote:
> I would have to agree that OOP is not a fad, perhaps over-hyped at times,
> but definitely not a fad. The argument about class dependencies is an
> invalid argument since functions will also have dependencies on other
> functions from other libraries, quite likely located in multiple source
> files. In fact, you've argued an advantage for OOP in the context of PHP
> since autoload can be used to mitigate this issue for classes, but not for
> functions. Another advantage of OOP that is difficult to provide via the
> procedural paradigm is polymorphism.
>