Blog Archive

Wednesday, 4 September 2013

Using Procedural Code in PHP

Object oriented programming (OOP) in PHP has become increasingly popular since PHP 5 was released, and especially from PHP 5.3 onwards. Without doubt, writing a large application using OOP in PHP has a lot of advantages over using purely procedural code (and if you are new to PHP I strongly advise concentrating your efforts on OOP). But despite the advances PHP has seen in OO support, it is still a procedural language at heart, and there are times when procedural is the way to go. Here are some examples...

When to use procedural code in PHP

  • All PHP scripts have to start with procedural code! Typically you have an 'index.php' file as the entry point to your application, and before you can use any objects, you have to instantiate them, and the first instantiation has to be done procedurally - there is no other option.
  • In most cases there are certain 'bootstrap' tasks that have to be carried out before you can process a request. This might include registering an autoload function, setting up error handling, and checking that the system is capable of running your application. All of this is typically done procedurally (perhaps also using static classes) because it is impractical to do it using OOP (for example, if you do not control the deployment environment, you might want your app to die gracefully even if someone tries to run it on PHP 4 - but if you try to use OO features from PHP 5, it will not be possible to die gracefully).
  • You could use procedural code to provide alternatives or additions to the built-in PHP functions (although you should first consider whether OOP would be better). For example, your script might or might not have access to multibyte string functions - it could be useful to have your own alternative functions that use multibyte functions if available or the standard PHP functions if not (you can use namespaces to override default PHP functions in PHP 5.3 and above, but this might not be very wise as your code will no longer behave in the way it would be expected to behave by any other PHP developer! Better to write separate functions with a different name).
  • If you are just writing an example script, a proof of concept, or a very simple script or plugin, there is probably no need to go OO.
  • If you prefer to write procedurally and feel you write better code that way, there is no reason why you shouldn't do so! Procedural code is not 'wrong' per se, there is just a tendency for it to lead to code that is difficult to test and difficult to maintain. But some great applications have been written procedurally, and it can be an acceptable choice, especially if you don't need to collaborate with anyone else. Better to write good procedural code than bad object oriented code!

When not to use procedural code in PHP

  • Pretty much any other time! Of course, I might not have thought of everything, so there are probably other occasions when procedural code is the right choice.

Other considerations

Object oriented vs procedural is not the be-all and end-all of programming models. I found this post about the 'messaging' programming model very helpful (at least the first part - it gets more biased towards MS Windows later on): Drastically Improving Your Code With Messaging as a Programming Model.


3 comments:

  1. I am not 100% with you on the 1st and 2nd bullets, you can avoid any single line of procedural code if you want to:

    class Foo
    {
    function bar()
    {
    echo 'Hello world!';
    }
    }
    (new Foo)->bar();

    But I agree with you on the 5th point: OOP is not better than procedural, it's only a matter of choice. It's why many PHP modules are available both through OOP and procedural APIs.

    ReplyDelete
  2. You could argue that the line (new Foo())->bar(); is procedural (albeit not functional) as the call is not made from within an object, which is basically what I meant, but you would be justified in calling that pedantic! The 2nd bullet point does carry the qualifier 'in most cases'.

    ReplyDelete
  3. Procedural code is way more short and readable than oop.

    If you need to do simple tasks procedural is good.

    ReplyDelete