Passing array/object as parameter?

Discussion in 'Programming' started by Triexa, Mar 26, 2008.

  1. #1
    As I work more and more with javascript frameworks I notice a lot use objects for parameters. I began to think, why not use this for my functions?

    Obviously if I just need 1 or 2 values, 2 parameters is fine, but when you get to more it can get a bit annoying (especially when you want to leave some/most as default). Even more, when you find out down the road that you need to add a parameter and sometimes just adding it to the end doesn't make the most logic.

    What are your thoughts on passing either an object or an array as a single parameter with all options set in that object/array? Then an array_merge to combine it with the default values.

    I haven't seen this done very much in PHP and for right now I don't really see why not.
     
    Triexa, Mar 26, 2008 IP
  2. mythbuster08

    mythbuster08 Peon

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    all methods have pro and cons.

    one con for passing single generic object and not single params: code is more difficult to understand.

    
    function ShowImage(string ImageName, int width, int height, optional int scale = 1)
    
    function ShowImage(object ParamObject)
    
    Code (markup):
    which is easyer to understand?
     
    mythbuster08, Mar 27, 2008 IP