Christopher Shennan's Blog

A day in the life of…

Over the last few months I have been working with Symfony and have got used to passing values to methods via an options array but I have recently had to pick up an older project in which the values are passed to the methods by individual parameters.

This has given me a good opportunity to evaluate both techniques and determine my personal preference for working.

The more traditional technique is to pass values to methods using individual parameters as in the example below:-

public function someMethod($name, $age = null)
{
  // do something
}

The benefit I have found with this method is that you can clearly identify what needs to be passed to the method, however, if you need to change the method to include an additional parameter it means that you have to update every call to this method so that every parameter is specified.

An alternative (and my personal preference) is to used an option array like in the following example

public function someMethod($options = array())
{
  $defaults = array('name' => 'chris', 'age' => 18);
  $options = $options + $defaults;

  $name = $options['name'];
  // do something
}

In this case, if you need to add an extra parameter to the method you can simply add the value to the options array and update the functionality within the method.  You do not necessarily need to update the calls to this method that are already in place  because they should still work fine provided the updated functionality is defined in such a way that it allows for the new parameter to be optional.

In addition, I feel that if you provide decent documentation regarding the options array (which should include a list of all the options values allowed), especially if it can be picked up via auto-completion in IDEs such as Netbeans, then this is just as easy to use as methods which are defined to accept individual parameters.

References

http://weblogtoolscollection.com/archives/2010/02/25/passing-parameters-as-variables-vs-passing-parameters-as-an-array/

About Christopher Shennan

I am a web developer specialising in web driven applications using PHP, MySQL, Symfony and Zend and I am currently working for Line Digital in Edinburgh, Scotland.

Most days I can be found frantically coding away with EuroDance in my ears and consuming what I hope to be a never ending supply of coffee... happy days!

Connect with me via Twitter, Google+ or LinkedIn

  • #1 | Written by jbreuer about 2 years ago.

    hi,
    first thing that came to my mind when reading your article was that the ‘options-array-method’ lacks the functionality of default values like in someMethod($someParameter = TRUE)

  • #2 | Written by Christopher Shennan about 2 years ago.

    Hi,

    I’ve updated my example to indicate how you could set the defaults. Basically you would have an array within the method which holds the default values and you would only specify any changes required in the options array you pass to the method. This is then merged with the defaults array so you have a complete array which holds all the required options for the method.

    I hope this makes it a bit clearer.

    Many Thanks

    Chris Shennan

  • #3 | Written by Rein about 1 year ago.

    Still you can’t do type hinting with the options method and unfortunately the $options do lack proper documentation many times. It’s one part of Symfony that has costed me more time than I think is needed.
    .-= Rein´s last blog ..Symfony Tips & Tricks IV- Generalized & Ajax Pagination =-.

  • #4 | Written by Christopher Shennan about 1 year ago.

    Hi Rein

    It is true, you can not do the hint typing and documentation can be lacking for the options array, especially when they are for custom methods but they can be extremely beneficial.

    In my case I was working on a project where I was using a method and it had 8 or 9 parameters and the last 2 were additional ones that I had to add. This method was used a few hundred times so I had to update a majority (but not all) of the calls to this method to set all the parameters to the defaults values just so I could set the 9th parameter to what I wanted.

    This also had the undesired effect that if I wanted to change the default value of any of the parameters I could not simply do this within the method because each call to the method had the default values manually specified, so again I would have to update a majority of the calls with the new default value.

    Using an options array would allow me to set these default values in the method and I would only have to override the ones I needed to, so I my case I would only have to give the value for the 8th & 9th parameter instead of all of them.

    I think it comes down to the personal preference of the developer when deciding whether to use explicit method signatures or options arrays and I tend to prefer to use method signatures where there are only 2 or 3 parameters to be specified and it is mandatory to specify these parameters, otherwise I tend to favour the options array.

No trackbacks yet.

Leave a Comment

Subscribe to comments

CommentLuv badge