Over a million developers have joined DZone.

Does Procedural Plus Dynamic Equal Object-Oriented?

DZone's Guide to

Does Procedural Plus Dynamic Equal Object-Oriented?

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

PHP has dynamic features, meaning that variables, functions, class members and even classes can be referred in a dynamic manner. Here are a few examples:

Example 1

echo $$a; //prints 4

Example 2

if($_REQUEST["param"] == “p1″) {
$a = “myfunc”;
} else {
$a = “myfunc2″;
$a();//either myfunc() or myfunc2() is invoked

Example 3

class A{...}
class B{...}

if($_REQUEST["param"] == “p1″){

$a=new $class(); //either an object of type A or an object
//of type B is instantiated

If we were to rewrite such examples using a language that does not have dynamic features, we could use a series of if-s to encompass all possibilities. For the dynamic variable case, there is only one variant - $$a can only evaluate as $b. For the case of the dynamic function call, myfunc and myfunc2 are the two possibilities. So an equivalent Java code would be:


The third example uses dynamic class instances and would translate in a similar manner:

Object a;
a=new A();
a=new B();

There’s something unnatural about these if-s, given the fact that Java is an object-oriented language.

In practice, dynamic constructs usually simulate object-oriented features such as polymorphism and inheritance. Myfunc and myfunc2 could execute different encryption algorithms on a string, e.g. MD5 or SHA-1. Classes A and B could encapsulate similar behavior (identically named methods) for working with different database types.

If we examine the problem from this angle, we see that a more natural solution would be to use polymorphism and inheritance the proper way - by extracting interfaces or superclasses and implementing the required operations:

interface I1{
class F1 implements I1{
class F2 implements I1{

$instance=Factory.getInstance(request.getParameter("param1").equals("p1"));//the Factory pattern

The third example is obviously the most awkward and the easiest to convert to an object-oriented approach, simply by extracting an interface or superclass out of A and B. Dynamic variables would however still have to be handled in an unnatural manner, for example by using reflection. A better approach would be to rewrite those parts from scratch and avoid the need for dynamic references altogether - e.g. by grouping variables into data structures, using semantic criteria.

In fact, PHP 4+ offers object-oriented features itself. They should be used in real-life projects to help maintain clarity, reduce the possibility of runtime errors etc. There are however many cases when applications use dynamic constructs instead - either for historical reasons or because the developers got stuck into the procedural mind-set.

Even if no migration to another language is performed, PHP programs would benefit from using object-oriented features instead of relying on dynamic construct “tricks”. In the case of a migration, the dynamic constructs have to be translated into static (probably object-oriented) ones. If the translation is performed automatically, the software migrator has to encapsulate appropriate algorithms - e.g. the extraction of common interfaces.

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}