Solved

pros vs cons of procedural vs Object oriented of php

Posted on 2014-02-11
11
8,215 Views
Last Modified: 2015-06-30
Hi,

im curious if there are pros and cons of both. it seems like procedural style is fading away and more and more leading to OO.
0
Comment
Question by:M. Jayme Nagy
11 Comments
 
LVL 18

Expert Comment

by:Jerry Miller
ID: 39850981
OOP is more of a modular approach and allows you to build a reusable code base that can be shared between applications. I wouldn't say that procedural is fading. It is still very useful depending upon what type of program you are building.

Here is a good description that I found on another forum:

I would choose OOP if:
 1.) There was a lot of code that could be shared and reused between the forms.
 2.) The data entry forms were anticipated to change often over time.
 3.) Many new data entry forms were anticipated to be added to the project over time.

I would choose PP if:
 1.) The forms were very unique and few elements were shared.
 2.) The forms were static and not expected to change much over time.
 3.) None or only a few forms were expected to be added to the project over time.

http://www.linuxjournal.com/content/programmer%E2%80%99s-discussion-procedural-vs-oo
0
 
LVL 27

Expert Comment

by:yodercm
ID: 39851078
The main problem with OOP is that it requires a lot of artificial constructs in the code that is widely used, to allow broad general use.

jmiller's description is excellent, and if you don't know that you *need* OOP for your application, avoid it like the plague.  It is harder to manage, harder to change, and any changes have to be tested against ALL using programs.
0
 
LVL 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 100 total points
ID: 39851176
Have to disagree there yoderm.

OOP allows a much more structured way of coding - it's not harder to manage, or harder to change. As for testing against all programs that use it, that's only if you've designed your coding badly from the start!

OOP allows you to write a set of code for a specific task, and encapsulate it all in it's own container. That container can then be used across lots of different applications. It can be extended (inheritance) and overridden (polymorphed) if needed, giving you far greater flexibility than Procedural. It also follows the DRY philosophy of coding (Don't Repeat Yourself)

Don't get me wrong, procedural has it's place, and if you're writing very small, simple apps, then proceduraly will probably be the way you go, but as you expend and add complexity, you'll realy see the benefits of OOP.

If you're serious about coding in PHP, then you will have to get your head around OOP. Database Management is heading towards OOP (PDO mySQLi) and away from Procedural (mySQL). PHP5 is fully OOP capable and include a lot of classes that will make your life easier.

It's not really a definitive pro vs con argument, because it depends on your needs. Sometimes you just need a simply procedural function, other times you'll really bebnefit from the power of OOP - learn both :)
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
ID: 39851200
Rather than say one is better than the other, I would just say that object-oriented programming solves a different set of problems.  At its most basic level, in PHP, object-oriented notation saves typing and reduces the risk of typographical errors when compared to using arrays for compound data structures.  You do not need to put quotes around object property names and that means you do not need to to wrap variable declarations in curly braces.  This notational difference alone saves me a few typographical errors and test cycles every day.

If you're asking yourself, "What can I do with an object that I can't do with procedural programming?" you're probably asking the wrong question.  The better question might be, "How can I make my programming more modular and reusable, so that other programmers can reuse my work and can contribute to my projects?"  

Consider this article's example:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_9854-Using-GeoCodes-to-Find-an-Average-Location.html

In the article we show how to create an object that you can use to compute an average location from a collection of data points.  Can you write this in procedural code?  Sure (inside the object you will find computations done in procedural code) but you can't extract the procedural code and reuse it in any other project without paying attention to the variable names!  That means a separate debugging cycle for every programmer who tries to use the procedural version and it negates any efficiencies that might come from code reuse.  So this brings us to one of the first and most important parts of object-oriented programming: Encapsulation and visibility of the properties (variables) and methods (functions).  You can take my class, put it into your library (you can even set up auto-loaders) and use it like a black box vending machine.  Put in what you want and get out what it gives you without any thought to how the innards of the object are working.  Never a care about the variable names.  Easy!

Now let's say that the class does almost exactly what you want, but you want to avoid the Haversine formula and use a more efficient plane geometry calculation for distances.  This is another obvious advantage of OOP: Extensibility and reuse.  You can extend the GeoObject class and provide your own definition for the compute_distance() method.  Nothing to rewrite and all other existing functionality remains intact.  Your only unit test will be the new compute_distance() method.  Easy!

Let's say that you're writing an application that does heat-maps from political opinion polls, and someone else is writing an application that tracks sensors in children's shoes.  The same class can serve both applications.  The heat-map class can be used to draw the maps, and the sensor application can be used to raise an alert if a child wanders out of a playground.  Of course you could develop these applications in separate and procedural code, but the debugging cycle would far exceed the time saved by reusing a common element of the code.  If you're a lone-wolf programmer nobody much cares, but if you're part of an organization that seeks efficiency from its software development staff, code reuse is very important.  Imagine bidding on software development projects when you had to start over from a clean slate on every project.  You would never win a bid because your competitors would have already built and debugged libraries of components.  They could do the same work in a fraction of the time.

I could go on and on (there are books written about this stuff) but hopefully you get the idea that OOP is all about solving the problems programmers have had for decades when working together on complex applications.  If you came to Object Oriented Programming and/or Object Oriented Design after you learned Procedural programming, the terminology and frame of reference may be confusing at first, but don't let that deter you from exploring the concepts.  The more you use it, the more you will find that it's a much better way of thinking about some kinds of problems.

Consider one of my favorite data structures, an array of objects.  With an associative array, you're pretty much restricted to a key and a value.  Of course the value itself can be an array, but what would you do if you wanted every data element to have some kind of initial processing as it was added to the array?  With procedural programming you would have to put the processing in line into the code that gathers the data and loads the array.  With OOP, you can isolate and debug the class definition of the objects, putting the processing into the class constructor.  Then as you add data to the array, your process of adding objects will automatically run the class constructors for each element of the array.  Your objects would naturally assume the form you wanted, just by the act of creating them with the new keyword.

Ref here: http://php.net/manual/en/language.oop5.php
0
 
LVL 18

Assisted Solution

by:Jerry Miller
Jerry Miller earned 100 total points
ID: 39851229
I don't think it is a pro / con argument either. They are simply different tools that can be used depending upon your needs. The right tool for the job makes your life so much easier! Most of what I write is in OOP using .Net, but I have written some basic structural in other languages as well.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39851291
@jmiller1979:

Electrician: "Apprentice, bring me a wrench."
Apprentice: "What kind?  Stillson, adjustable, pipe, lock, monkey..."
Electrician: "Doesn't matter.   I'm just going to use it as a hammer."
0
 
LVL 45

Assisted Solution

by:aikimark
aikimark earned 100 total points
ID: 39851686
You might distill this argument into the simple phrase "code packaging".  Once you get out of your line-by-line coding mentality, you refactor your code into units that you can invoke (subs and functions).  You can package these invocable routines into methods of one or more classes.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 200 total points
ID: 39851702
Good comment by aikimark!  You may also note that you cannot redefine native procedural PHP functions because a fatal error will be raised.  But in OOP, the parent classes can create functions that may be redefined by the child classes, making the language inherently richer and more nuanced.
0
 
LVL 6

Author Closing Comment

by:M. Jayme Nagy
ID: 39851734
thanks guys

It definitely looks like procedural will become a thing of a past as like ray said OOP makes the language more richer
0
 
LVL 8

Expert Comment

by:F P
ID: 40860825
PHP is a top down / left right scripting language, and tries its best to be OOP. Shame it truly can't be the way the web is architected currently. Java / Javascript / Ruby / Python ... and so on. Those are OO. When PHP makes the jump into C extensions like it has been, e.g., HHVM, PHPNG, and other JIT or C style compilations, then it could be OO, but not as an interpreted scripting language that it is which forgets it existed every time it's invoking a script and is mainly (I've written apps that are persistent and it can't do it correctly or keep up with Java) used without persistent connections...

The reason you should learn procedural when OO style is available? When I learned programming, it was using the CL on IBM AS/400 - they invented SQL before Oracle - and that was a procedural language which loved to use GOTO. Similar to vbscript or batch files in Windows. It's 2015, and you shouldn't think that a language developed in the early 1960's has a place in anything you're learning today for the same reason you don't put faux wood paneling on a wall instead of drywall. They accomplish the same thing, but it looks ugly and people will look at you funny while valuing your input less.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
This article discusses how to create an extensible mechanism for linked drop downs.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now