Portal Home > Knowledgebase > Articles Database > PHP Debate: OOP vs Not OOP


PHP Debate: OOP vs Not OOP




Posted by Floridaserver, 11-22-2007, 04:03 PM
I have been programming for about 4 years now, mostly PHP and MySQL. I started without the use of OOP and just hard coded, made my own functions, and learned how to streamline and simplify. In that time I did many side jobs to help others out, some free, some for pay, and mostly it was to reprogram things like OScommerce, forums, and other bug open source PHP programs. They all used the smarty template system and OOP, or just OOP. I found the code to be very bulky, bery longwinded. When I did these programs all I could think of was why they wasted so many lines of code when they could have spent 1/4 of that to make things work...and make it easier to modify. Now, if I am building my own big program for me, then it is how I want it to be...but if you are throwing an open source, you can expect everyone and their mother want to modify it. I just find OOP to be very very wasteful, time consuming, and just plain ugly programming. I have worked with OOP programmers and found, at least with the ones I deal with, that they cannot program their way out of a paper hat. They seem to be advanced 'alterers' or 'modifiers' more than programmers. Now I have met some OOP people that just make my head spin and have been doing it for a decade..but that is with prgramming like C++ and stuff....headspinning to watch them. But regular, non OOP, is so quick, so adaptable, so easily modified... I just find it a better way to program So.....do you only OOP or do you hard code? Do you hate OOP or can only OOP? DO you find OOP programs to be a biiger overhead or less? Why did you go OOP and what do you think makes it better.. I am willing to look at it again incase I missed anything...lol

Posted by dotBoost, 11-22-2007, 08:31 PM
Well, OOP is for Web developers. Web programmers can stay with procedural PHP , of course , and when or if they become developers, and after they will work at some big projects, where OOP is a must , the difference will be obvious.

Posted by Floridaserver, 11-22-2007, 09:33 PM
I do not know...every OOP project I was given was nothing but a monstrosity and just poor programming. OOP for web development in PHP is probably just as good as procedural, it may just come down to the gift of the programmer. I can see where if it is a big project iwth many workng that OOP may be necessary, but I find it is usually because the skill of those programmers are not very good...they are usually block movers and not programmers... I guess on the top tier I can see where it may or may not matter..but for many programs I have to fix the worst are ALWAYS OOP...slower, massive memory hogs, and just poor planning and layout. I find procedural allows you to economize, clarify, and customize...and many of the shortcuts for OOP are just as easily done with a simple function. I don't know..if I could find one program that was a very good optimized build that was OOP to look at and see how it would be better...I would be open...but so far I have found none. I guess by large project you mean?....I have no idea since most web apps, no matter how large they appear..are just not that big at all...(not PHP ones anyway). I have beeen reading online and talking to programmers...but I have not really met a programmer of significant skill that uses OOP...I have met a ton that have bad skill and use it only. Even PHP5 is not really a full fledged OOP programming language and I think I am sticking with procedural...especailly for overhead, security, and ease of flowing code. Web develpoer versus web programmer...intriguing difference.. I develope massive CMS systems and others...so I would assume I am a developer...I use procedural. It has been just better and easier for me to use my pre made functions and my flow, then some badly formed tetris version of pages and scripts...ugh. But, again..I think it just depends on the level of the programmer...and whether you make it easy to modify for others or users...

Posted by debiannerd, 11-22-2007, 11:18 PM
OO with PHP4 sucks but rocks with PHP5 - OO will boost your coding and extend it. I think all your impressions are not negative because you just don't simply understand OO coding... maybe you should wonder why it is widely and heavely used among professional coders. For example, don't you think it is much more cleaner to write than

Posted by Floridaserver, 11-23-2007, 12:44 AM
I don't know that many good programmers taht do use OOP with PHP...C yes...not PHP... terms like this " mysqli_query" do not have to be written but once as you would use a function for that like....f(select from... ame thing with all common terms like mysql_num and all that...just make your own custom abbreviate functions.... and you will be typing and erroring a lot less. It is also cool to make..and I guess you could do it with OOP just as well...but use a little coding up top for going through table names and outputs...no sense in typing those things out all over the place all the time either. Less typing, cleaner code...that is what I am all about...simplify, clarify, economize. I am sure a great programmer is a great programmer...but the huge amount of OOP coding I have to fix, repair, rebuild for web accounts is just annoying... I wish they would learn it better before they built it..but most projects I see are justpoorly designed...period. just turns me off of OOP...too many bad people use it...ugh

Posted by riscphree, 11-23-2007, 02:03 AM
Most of my stuff has been procedural for the most part. I'm slowly moving to OOP because it is more streamlined and extensible. When I'm writing an app, I always plan for the future and what I might want to do later down the road so I try and keep it easy to add stuff. I've found that OOP has been very helpful with that.

Posted by Steve_Arm, 11-23-2007, 02:31 AM
There, you said it yourself, you hadn't met the right people yet! Go with what you feel more comfortable in. For small projects there is no need to go fully oop. But in a ig project where there are many people making it, you need it. The purpose of oop is to objectify your application and bring it to a level where you need to change only whatever needs to be changed. Sometimes I create the front end completely oop with an mvc design and do the backend straight one page code. This is a big chapter. Main reason for this happening is that most people getting into web development don't have fundamental programming education.

Posted by Floridaserver, 11-23-2007, 03:12 AM
I kind of like the challenge of making a super lean and easily modifiable script..it is not always easy. I just remember in 1979-80 doing basic programming on the apple... We were taught quickly that a miximum effort on keeping code to a minnimum also kept us on our toes to program as well as possible. The trs-80 had 4k memory and a casette tape to record as a disk...very little room..lol With todays huge ram, cpu, and disk space, it seems most completely ignore that fact.. But then I see a lot of memory hog programs and people having to upgrade their servers too.. Like you said...I have not physically met a good OOP PHPer yet. Though I have many C++ friends. Most open source code I see is just memory hog and overhead heaven. Man... Looking on you tube as a joke one day..I found real cool basic php and html videos...hilarious... here is a php calculator one.. http://www.youtube.com/watch?v=BRd4xguBX6A the best part (after you expand the video to read it) is the voice over...Brings back memories of early thought processes (and todays too) when programming... I found it amusing...and cool

Posted by Steve_Arm, 11-23-2007, 03:24 AM
Wow that video... talking about promoting bad practices!

Posted by riscphree, 11-23-2007, 04:03 AM
lordy, that video made me cry

Posted by Floridaserver, 11-23-2007, 05:57 AM
man, it was great..reminded me of early thought processes when I started on my own...

Posted by Karl Austin, 11-23-2007, 07:09 AM
OOP all the way. Any code done by a poor developer will be bad, but OOP done right is going to be so much cleaner and easier to maintain than procedural code done well. OOP is worth it just for encapsulation alone, never mind anything else.

Posted by sasha, 11-23-2007, 10:17 AM
I never saw pure PHP OOP application. I see that as PHP strength, ability to use the best tool for the job and mix structural with object oriented programming. The most of my code looks like this, for each distinct part of project (frontend, backend) there is single controller file that has all logic in it. As appropriate that code will use objects simply because that makes things easier to code and more portable between projects. Here is a quick example to give you an idea (i will just type it in here as illustration, my actual code is a bit different): Benefits of this are: - When new project comes in, I can use 100% of that just the way it is and that saves me lots of time. In fact I have a script that I run that copies and adjusts basic project framewrok which includes shopping cart, user management, CRM and CMS - Whoever does design can do design/html/css and needs to know nothing about php code. - The code structure does not have to be the same as the actual output. For some reason some place in the middle of things, based on some arguments I could decide to output content as JSON structure (for use with AJAX) rather then outputing entire page. Something along these lines: Here is another example, rather the coding same stuff over and over, you can reuse parts of classes and write only stuff that is specific to current project. That keeps things interesting.

Posted by Floridaserver, 11-23-2007, 12:33 PM
I have the same things...most security, login, stats, etc...are all reusable.. I do not and never will use smarty templates...ahhhh!!!! But I have a core set of functions and code I reuse all the time..I guess it is the same in a way.. and like they say, doesn't matter much in small projects or your own.. however, I know of no LARGE php program that would demand it. I just look at oscommerce, phpforums (all of them), and a lot of the CMS and all I see is horridly wasteful coding...KISS might be the name of the CMS system I make just to show em...(Kiss= keep it simple stupid). ugh...I did some work for someone that wanted to use Oscommerce recently...wow...if ever a program of breadth was not planned and coded piece meal it was that one... ---> banging head against wall for spending one minute fixing that OOP monstrosity..... but...I guess it was free for the owner..except for all the cash he shelled to make it adapt to him (something that could have easily been built into the Commerce system...just was not planned that way.) yea...think I am gonna make a slim powerful, easy to modify and add to procedural CMS system.... wonder how little number of pages I can do it in...I think 10-15 would do it...might be a fun open source project...just for kicks.

Posted by webshack, 11-23-2007, 02:46 PM
Best Practice would be combine those too,... Rule of thumb would be; small projects - structural large projects - OOP Realistically whatever approach will help you to deliver a project on time is best possible solution.

Posted by vpsville, 11-23-2007, 09:18 PM
This same argument happened in the mid 80's when C++ came out, all the ANSI C programmers hated it. The fact is that OOP is a powerful tool. If you're a bad programmer, that means you'll be able to finish your awful projects faster. If you're good, which is rare in the PHP world, it will help you too. PHP is not C++ of course, but you can do beautiful things with OOP in PHP5. I've been a C++ programmer for about 16 years now, and I still use C sometimes for small apps. But for a large website I would use OOP, no question. Classes are much more portable between projects than functions. I hate writing anything more than once and I'm sure you do too. For larger apps, you want the structure and organization that OOP gives you. There is a much larger investment with OOP, you have to plan your classes and sometimes write classes and member functions around things that seem trivial, but they can help you later. The old saying 'well begun is half done' comes to mind. The planning and upfront work of OOP is almost always worth it, unless your program is trivial (and most web sites probably are). I suggest you study OOP, look for small examples that show the power of PHP5. Then try to write a small site in OOP to see how you like it. Here's an example of how OOP is great: Say you access a database, today its MySQL. You could use mysql() functions in php, or you could write a wrapper class. If your whole site uses the class, you can change your site to use Oracle just by changing your database class...which is going to be one file. Hope this helps;

Posted by triXtyle, 11-23-2007, 11:41 PM
Yep, or small projects - structural large projects - structural + few beers just kidding I prefer not OOP , because I hate OOP since my previous exp. with C++ I was on the following moto: C++ program - run, C++ program - crash, C++ programer - cry cheers Last edited by triXtyle; 11-23-2007 at 11:44 PM.

Posted by jap4cpanel, 11-25-2007, 07:08 AM
You should also keep in mind that PHP is not an OOP language in origin. PHP4 introduced some OOP principles, but in my opinion those were insufficient to go OOP. PHP5 has made enormous leaps forward regarding OOP support, enough for me to choose the OOP approach. Yet a lot of frustration remains: no support for namespaces, no parameter overloading, etc .. In my experience the lack of for example parameter overloading, can result in some pretty horrific code. But whos fault is that? You can just as easily blame the language for not supporting a basic OOP principle. OOP programming requires a lot of planning, but when implemented correctly it can be your best friend (during development and maintenance). I agree that a good implementation requires a good programmer, otherwise it can go very very wrong (you can find lots of examples of that). One of the things I always keep in mind when programming OOP in PHP: keep it simple stupid. It sounds simple to do, but it isnt always like that :p

Posted by Jamie Edwards, 11-25-2007, 07:26 AM
If you program OOP correctly, you end up with less code (that is the whole point) and something more flexible, that removes the domino effect of changing code later on (that with NOOP would break something further down the line). A language is never 'OOP' or 'not OOP' - it is the programmer. The whole point about OOP programming is that it makes your life easier - less code to copy and paste, less code to maintain, easier for others to use and for you to revisit, and allows you to put more of a context on your program so it is easier to understand. If you think it is bloating your code and giving you more work, then you are not programming OOP or understanding it fully.

Posted by Codelphious, 11-25-2007, 09:05 PM
I agree completely with Jaime above (well, except for .. which is simply not ture). Those that understand OOP will always choose to use it. If you're doubting the benefits of using OOP then you need read up on it. And for those of you that have been championing procedural programming for small projects: why write less maintainable, less reuseable, and generally sloppier code? OOP is an advancement on procedural programming (read up on programming paradigms)... if you're not willing to adapt to the future, you might as well be coding in assembly. Obviously, I'm speaking in context here and not in general. Let's not forget there are numerous different languages and paradigms that work exceptionally well for their niche.

Posted by luki, 11-26-2007, 03:17 AM
The OOP capabilities in PHP5 aren't excellent, but they are decent. If you take advantage of them, you do end up with less and more portable code. It's simply self-contained (classes) and does not contaminate the global variable and function space. Take advantage of overloading, class extension, etc. By taking advantage of the __get, __set and __call methods, you can write quite intelligent code that, for example, only fetches data from the database when it's needed to be displayed in a template. No need to pre-fetch all data that could be needed (think collapsible and user-defined content on a page, dynamic loading of parts with AJAX, etc). You can even write "auto-committing" classes that save their data to the database on destruction of the instance, without the need to explicitly commit changes. Yes, with proper coding this can be very efficient (works well for me). And yes, a template engine is a must, except for very small projects. I initially hated smarty, but then I started to love it. Now 6 years later I still love it. It's powerful and it scales. I recommend it. Last edited by luki; 11-26-2007 at 03:21 AM.

Posted by jap4cpanel, 11-26-2007, 03:34 AM
A comment on Smarty and why I, personally, do not use it. Why would I want to learn a scripting language who is based on another scripting language, because that is basically what Smarty is in my eyes. Why not just use PHP? You might argue the template designer does not know PHP, but does he know Smarty? Learning (a subset of) PHP takes just as much time as learning Smarty. Yes Smarty offers a lot of helper functions, but you can just as easily make those with PHP. Smarty prevents the template designer from messing up PHP code? Use the MVC pattern and that is not a problem. There are probably lots of arguments in favor of Smarty, but for now I feel PHP gives me everything I need.

Posted by Floridaserver, 11-26-2007, 08:03 AM
I totally agree. Whenever I see a smarty application I knwo I am in for a seriously flawed code, poorly designed project, horrible end-user (website owner) interface. Programs such as these usually have tons of forums with people pleading to get teh simplest modifications done... I am sure a good programmer can make use of it, but I have never seen a smarty system yet designed by one. Why do in 100 pages what you could do in 3? Bad programmer + OOP project + Smarty == $killself

Posted by Jamie Edwards, 11-26-2007, 09:05 AM
It is true - it is easy to do OO C, and a lot of people do. People have been writing OO PHP since PHP3, and so on. Don't go blaming the teqnique; blame the programmer!

Posted by vpsville, 11-26-2007, 10:42 AM
This is true, you can even use OOP when writing assembly. I suspect that many experienced programmers that write excellent procedural code are actually using OOP principles without knowing it. It just comes natrually as you write more and more code and seek to reuse it. OOP languages certainly make that easier and cleaner. PHP5 was a big improvement but its certainly still clunky compared to C++. Take the $this->value for example. You have to use $this a lot when writing PHP5

Posted by Jamie Edwards, 11-26-2007, 01:31 PM
You don't have to - like you don't have to in Java or C++, but it is often better to be explicit and use $this-> even where not needed.

Posted by vpsville, 11-26-2007, 03:16 PM
Umm...wrong. Variable scope is quite primitive in PHP5, so when addressing a member variable within its class in a member function $this->variable is required. Its not optional. If you don't use it you'll be creating a new variable and referencing it instead. If you don't believe me try it. In C++ it is optional, meaning its only used rarely and not to add clarity. Good variables names and comments add clarity. Using the same variable name in several different scopes and using $this-> is not what I would call clear. I haven't used Java much since 1.0 so I can't comment on its use there

Posted by brandon272, 11-27-2007, 04:34 PM
I think it's entirely based on personal preference. When I was learning how to program I had trouble with the concept of OOP. Or I should say, I understood it in concept but didn't agree with a lot of it. But I was able to wrap my head around procedural programming really well and took off with it.

Posted by Ryan F, 11-28-2007, 02:32 AM
You don't agree with it because you don't understand it. It's a complicated subject and not one that is well covered by the so called experts in the PHP world as I don't think they really get it either. There's a reason why most mainstream languages are fully OOP (JAVA, C#, VB.NET etc). If you want to make programming a career or just learn to write professional caliber code I'd encourage you to learn one of the languages I mentioned and then, if you still want to continue using PHP, apply the principals you learned to your PHP applications. O'rielly has a book called "Head First Design Patterns" that does a really good job explaining OOP. If you have an O'Rielly account you can read it on Safari.

Posted by Codelphious, 11-28-2007, 03:20 AM
I don't think you quite grasp the concept of object orientation. Obviously objects can be represented in C, or assembly, or even binary. In fact, all OO programs *MUST* be converted to binary (aka "machine code") in order to run, no matter what the language or paradigm. The concept of OOP is intended to make things easier for us -- humans; so that we can better express our intentions. Programming languages are written by men, for men, to help men communicate with machines. Different languages fall into different paradigms or classification, based upon their level of abstraction and (loosely) upon their lexicon. The more abstract we go Binary -> Assembly -> C -> C++ -> PHP the easier it is to express our intentions in English as programmers. Some languages are OO and some are not depending on how they were designed. Assmebly and C were not designed to be OO, writing OO code in Assembly or C, while possible, produces spaghetti code with gotos everywhere and/or overly complex syntax that is not maintainable and not readable. These are the basic concepts of OOP -- make things easier for humans to understand.

Posted by Ricjustsaid, 11-28-2007, 10:36 PM
Same here. I started off learning Java and had trouble grasping the idea of OO and what the purpose was. That was probably because I didn't really do anything worthwhile with it. Then I taught myself PHP and started working on my own projects, and realized 2 things: my procedural programs were quick to write, but making changes to them sometimes had weird effects; and OO was a bit slower to write, but easier to change, a lot easier to extend, and seemed like the right choice for me. Look at JS frameworks like Mootools or jQuery... they're OO and allow people to write their own custom classes to do really cool stuff. I don't think it would be possible to do without OO.

Posted by Jamie Edwards, 11-30-2007, 09:58 AM
Absolutely correct!

Posted by TonyB, 11-30-2007, 05:04 PM
For a large project doing it procedural seems absolutely silly at this point with PHP4 being EOL'ed and PHP5 having decent OO features. But a lot of people will argue procedural but I question if they've worked on a project for an extended period of time with numerous developers.

Posted by ans_steve, 12-04-2007, 12:20 PM
OOP with MVC is the best option. I do all my projects using Zend Framework with Smarty engine, once you get the hang of it, you will never go back. It does take a bit more time for planning. Also the best thing I like about OOP is once you grasp it, it is actually more fun to do programming. I did procedural for many years, but so glad I took the time out to learn OOP properly.

Posted by Codebird, 12-08-2007, 04:44 PM
I agree 100% with the rule of thumb of webshack, I think oop for small projects isn't but a waste of time and structural for big projects is a suicide



Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read
LiquidWeb Down AGAIN (Views: 763)


Language: