Re: Object Oriented Programming question

From: Date: Tue, 19 Jan 2010 18:12:49 +0000
Subject: Re: Object Oriented Programming question
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-301225@lists.php.net to get a copy of this message
Ashley Sheridan wrote:
On Tue, 2010-01-19 at 12:30 -0500, Paul M Foster wrote:
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
I wouldn't call OOP a fad really, as that suggest a silly short-term trend, but you do bring up a good point about class dependency problems. If dependencies are causing that sort of level of problem, then it might be worth re-making part of the wheel (after all, you wouldn't see wooden cart wheels on a Ferrari, would you?!) I've seen plenty of sites use no OOP at all, but I think once a system gets beyond a certain size, it does make more sense to use OOP, just to avoid running into problems where you have so many functions performing so many tasks but becoming a nightmare to maintain.
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. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP

« previous php.general (#301225) next »