From: Ben Stones on
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,
From: "Bob McConnell" on
From: Ben Stones

> 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.

OOP is a way of looking at a problem and map it into code. Some problems
will fit into it, some don't. Some people can look at problems and see
objects and some can't. But in most cases, it is not easy to take an
existing procedural program and re-map it into objects. It would be
easier to start over from the specification and write it from scratch in
the object model.

If you have been doing procedural programming, don't worry if you don't
figure it out right away. It is not an easy transition to make. Many of
us with decades of programming behind us will never be able to make that
switch. Our brains are too tightly locked into the previous thought
patterns.

Bob McConnell
From: Floyd Resler on
Ben,
I use a combination of procedural and OOP in my scripts. It depends on my needs. As an example, I use OOP for gathering order information. I created a class which gathers all the order information allowing me to easily access any piece of data in the order. I could do this with a bunch of individual functions but it's easier and more logical to group them all together in a single class. When I first started working with classes, I used them sparingly. Now I find myself using them more and more.

Take care,
Floyd

On Jan 19, 2010, at 10:11 AM, 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.
>
> Thanks,

From: Ashley Sheridan on
On Tue, 2010-01-19 at 15:11 +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.
>
> Thanks,


There are a few advantages by going the OOP route with PHP.

Firstly, you can bring together what would normally be in several
variables into one object, making it easier to work with, instead of
having to use various global variables to hold all the information.
Also, you can instantiate more than one object of the same type, and it
is a lot easier to work with than global variables. For some complex
cases, you could only manage this with a huge global array, which would
become unwieldy if made too large.

Secondly, object allow inheritance. So, if you create an object class,
you could use that as a basis for another more complex object by
building upon it with inheritance. You can also use other classes as a
basis for a more complex one of your own. Pear does this quite a lot.

Lastly, with OOP, you can create functions that can only be called from
within your object. These methods allow you to create functions that
will only ever be used by your classes, and won't accidentally be called
from anywhere else in your scripts.

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


From: Paul M Foster on
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