PHP - which is better: object oriented or procedural?
If you want to skip the background stuff and cut to the chase, click here.
There are literally thousands of posts around the Internet on this question, and a lot of the time people seem to side with the coding practice they are most comfortable with…. understandably, but is it really an all or nothing scenario? should you code using just one ‘method’ (sorry couldn’t resist), or the other?
Users of Java, Ruby and other such object oriented languages, will quickly rebuff the proceduralists as unprofessional and intentionally or otherwise, make you feel as if you are an ‘illegitimate’ programmer/coder. If you have been coding PHP in a procedural format for many years, you may be beginning to feel you are quickly becoming ‘yesterdays news’, but is this a fair assessment?
PHP of course is a scripting language designed specifically for the www, though of course it is employed on many thousands of Intranets around the world, but it’s primary focus is on the Internet. Going back fifteen years, websites were smaller on the whole, and definitely less sophisticated, and websites that used PHP were (through lack of choice in PHP versions prior to PHP4), strictly procedural.
The way people used the Internet back then (and now to a certain extent), meant that procedural PHP was fine, it fitted the bill, so to speak. However with the advent of PHP4 which offered an object oriented path of sorts, and the introduction of other object oriented online languages, the online coding world began to change, but with this change, does this mean the ‘old’ procedural way of coding has now become obsolete?
Absolutely not. Hundreds of thousands of PHP coders have been and continue to write, excellent code employing the procedural style. The ones that use good programming practices see little reason to go the steep learning curve into object oriented PHP, and rightly so. If the goals of any website being developed can be written well using procedural style, then there is no need to change.
The Bottom line
If you are part of a team, programming is your job, or you want to start a career in web development, and/or the website you are building is a large and complex database driven application, then it is almost certain that object oriented programming will be a fundamental requirement, particularly if skills are broken down into specific ’skill sets’ ie, developer, graphic designer, database logic.
If you work on your own, or are involved in building small websites then procedural programming will be just fine. Don’t forget, procedural code has the added benefit of faster execution times than object oriented code.
For me personally, I let the projects’ requirements determine which approach is required, though I do find a combination of the two is often helpful.