Re: Object Oriented Programming question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Jan 19, 2010 at 03:11:56PM +0000, Ben Stones 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.

<opinion>

OOP is a *trend* or *fad* in programming. You can create a whole
application written almost entirely with OOP. It will usually follow the
MVC (Model-View-Controller) paradigm. It will have a front controller
(one page that every other page starts from) and distribute the work of
displaying pages, validating values, and storing data across a variety
of classes in a bunch of files. See a package called CodeIgniter for a
good but simple example of this paradigm. Generally, an application
written this way will load many tens of pages' worth of code before any
byte gets to the screen.

Alternatively, you can write simple OOP components for selected parts of
your application. For example, if you're dealing with a bunch of
customer screens, you can write an object oriented class which handles
all the customer queries with the database.

There are a variety of arguments that OOP advocates will make in favor
of OOP. It's *supposed* to make your programming easier and faster, and
make for easier debugging. In the real world, this may or may not be
true. OOP does work to reduce the clutter in your namespaces-- the
names of methods within classes are hidden from the rest of your
namespace, unlike global functions. It also relieves you from having to
pass a lot of parameters to each routine you call; you can store a lot
of properties in the class, and they are shared with the methods in the
class.

One of the more serious problems I've seen with OOP lies with classes
which have dependencies on other classes. It's like walking a minefield
sometimes, ensuring that this class gets instantiated before that one,
which depends on it. You can write an incredibly complicated automatic
instantiator which instantiates classes and ensures they fire in the
proper order (I have), but it seems kind of silly when you could just as
easily write external functions which perform similar functions.

</opinion>

Bottom line is, study OOP (look it up in wikipedia.org), and if you
think its advantages are worth your effort to learn the new paradigm, go
with it. But ignore the hype (and there's a lot of it). Do what works
for you.

<suit status=on type=flame-retardant>

Paul

-- 
Paul M. Foster

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux