DISQUS

Perplexed Labs: PHP Named Parameters

  • david g. · 7 months ago
    in the switch at line 64 -- how do I pass in parameters whose type are Object? It appears to be only testing for simple types (strings, bools, numbers ...) which are known.

    how is this clearer than just making every function in your object have a signature such as:

    function foo($opts=array()) { return $blah; }

    yes, you get some default named parameter value handling from some boilerplate code ... but it appears you're limiting what kinds of parameters you are able to pass around ... imho this looks like over-engineering that'll lead to coding yourself into a corner.
  • Matt · 7 months ago
    @david

    Because PHP doesn't natively support true named parameter passing this is mostly a "hack".

    Also, because PHP is loosely typed and you can't specify that a certain method parameter only accept an Object, I can't think of any way to handle that special case.

    I wouldn't suggest this solution be adopted for general programming in PHP however there are some specific cases where this can be very useful. One of them would be in the creation of Rails like helper methods for a view (which is where I'm using it now in my own projects).
  • iongion · 7 months ago
    Why wouldn't array solution just fit your needs ? just because you have to type in "array" construct ?

    This is not python, it is PHP, get used to it having his own ways(most of them bad ways)
  • Matt · 7 months ago
    The array solution makes code more difficult to read - this is more succint.