PHP Functions Using Named Parameters - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Functions Using Named Parameters - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Thursday, May 23, 2019

PHP Functions Using Named Parameters

PHP Functions




Using Named Parameters

Problem

You want to specify your arguments to a function by name, instead of simply their position in the function invocation.

Solution

PHP doesn’t have language-level named parameter support like some other languages do. However, you can emulate it by having a function use one parameter and making that parameter an associative array:

          function image($img) {
               $tag = '<img src="' . $img['src'] . '" ';
               $tag .= 'alt="' . (isset($img['alt']) ? $img['alt'] : '') .'"/>';
               return $tag;
          }

          // $image1 is '<img src="cow.png" alt="cows say moo"/>'
          $image1 = image(array('src' => 'cow.png', 'alt' => 'cows say moo'));

          // $image2 is '<img src="pig.jpeg" alt=""/>'
          $image2 = image(array('src' => 'pig.jpeg'));


Discussion

Though using named parameters makes the code inside your functions more complex, it ensures the calling code is easier to read. Because a function lives in one place but is called in many, this makes for more understandable code.

Because you’ve abstracted function parameters into an associative array, PHP can’t warn you if you accidentally misspell a parameter’s name. You need to be more careful because the parser won’t catch these types of mistakes. 

Also, you can’t take advantage of PHP’s ability to assign a default value for a parameter. Luckily, you can work around this deficit with some simple code at the top of the function:

          function image($img) {
               if (! isset($img['src']))      { $img['src']       = 'cow.png';        }
               if (! isset($img['alt']))       { $img['alt']       = 'milk factory';  }
               if (! isset($img['height'])) { $img['height'] = 100;                 }
               if (! isset($img['width']))  { $img['width']  = 50;                   }
               /* ... */
          }

Using the isset() function, check to see if a value for each parameter is set; if not, assign a default value.

Alternatively, you can use array_merge() to handle this:

          function image($img) {
               $defaults = array('src' => 'cow.png',
                                           'alt' => 'milk factory',
                                           'height' => 100,
                                           'width' => 50
                                          );
               $img = array_merge($defaults, $img);  
               /* ... */
          }

If the same key exists in the arrays passed to array_merge(), then it uses the value in the later array. In the preceding example, that means that any values in $img override values in $defaults. But if a key is missing from $img, the value from $defaults is used.


No comments:

Post a Comment

Post Top Ad