I am a programmer and architect (the kind that writes code) with a focus on testing and open source; I maintain the PHPUnit_Selenium project. I believe programming is one of the hardest and most beautiful jobs in the world. Giorgio is a DZone MVB and is not an employee of DZone and has posted 637 posts at DZone. You can read more from them at their website. View Full User Profile

What paradigm should PHP applications embrace?

09.10.2010
| 7486 views |
  • submit to reddit
What should be the dominant paradigm for PHP?

PHP is one of the most used web development programming languages in the world, and from its original creation in 1995 has evolved much both as a platform, with drivers for nearly every existent database, and as a language itself, with the introduction of several new programming paradigms.

At first, PHP was all procedural and proposed structured programming as a solution for every business problem. In this period of its life, PHP was still a language for interactive documents as web applications did not exist yet. Content Management System and open source products like Drupal, Wordpress and phpBB made their fortune on their feature's quality, built over a procedural Api. But also most of the legacy code we work with now in 2010 follows the procedural paradigm, as it was the first paradigm supported.

With PHP 5, released in 2004, came the support for object-oriented programming in PHP. In PHP 4, the object engine was a joke, while PHP 5 borrowed much of Java's paradigm and transplanted it into the environment of a dynamic language. It was this shift that makes PHP frameworks possible now.
Every modern PHP library built with userland code we encounter offers an object-oriented Api. Contrast this with C extensions, which usually are still based on primitive functions, with quite a few well-thought exceptions like PDO and SPL.

In 2009, PHP 5.3 become the latest official, stable branch, and between its innovation we find an implementation of the functional paradigm, with anonymous functions and their specialization as closures being now supported by the language itself. Functional programming is based on functions instead of classes as the basic unit of computation, but treats them like first-class citizen by passing them around in variables and composing them dynamically. Functional programming also targets parallelization, due to its natural lack of shared state between functions.

But with PHP 5.3, also the invulnerable phantom of spaghetti code haunted PHP developers: the GOTO construct was now supported. The limited ability of make a certain subset of jumps was now in the hands of a population of developers that mostly lack a formal training.

Thus PHP supports nearly every programming style you can imagine. What do you feel should be the direction of the language and its APIs? What will become the dominant paradigm in the next years?
Procedural (C-like)
6% (18 votes)
Object-oriented
73% (216 votes)
Functional
13% (40 votes)
Spaghetti-like (GOTOs)
8% (23 votes)
Total votes: 297
Published at DZone with permission of Giorgio Sironi, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Mario T. replied on Sat, 2010/09/11 - 1:34pm

PHP5 is still a joke in regards to object-orientation. The criticism around PHP4 wasn't the lack of private/protected methods. It was the lack of first-class functions and methods. The requirement to create manual object references was merely cumbersome. 

PHP5 just rectified the object references, even though that was never a real problem. And it brought various new magic methods (of which the __constructer is redundant). But that doesn't bring PHP on par to other OOP scripting languages. Especially the presence of purposeless private/protected attributes and methods makes it a running joke among Python/Javascript/Smalltalk communities. This is a feature that makes sense in compiled code (namely C++ and Java), but is totally cargo-cult driven in interpreted languages.

The only serious addition in PHP5 were Exceptions. The only exception about the seriousness of Exceptions is that none of the core functions uses them. The SPL functions bring a few useful features to PHP. But even those are rarely utilized. The only thing I frequently see in "PHP5 compatible applications" is PHP4-incompatibility, by virtue of runreasoned attribute privatization.

Giorgio Sironi replied on Sun, 2010/09/12 - 10:07am in response to: Mario T.

Why do you feel that private and protected scopes do not have a place in interpreted languages? They are really about encapsulation, and I daily thank everyone who has kept a methods with an as strict as possible visibility: for example I can easily refactor a private method by looking only at the source code of its own class, being sure of not breaking any other client code. In fact, Python is interpreted and has private methods.

Besides that, the OO paradigm of PHP 5 borrows heavily from Java, and with its object handlers system gives the possibility of building large object graphs like the ones of frameworks such as Zend or symfony. There is still much work to be done however - SPL really sucks from a design point of view (its component are used for performance), and things like collections or lists still have to be created in userland code.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.