I got a comment by Adam Kramer, whose blog I linked on the original article about PHP named parameters, saying that would be cool to have a mix of default and named parameters.

So I made a nice cup of coffe and a few hacks later, came up with a way to do it, and went to tell Adam. Well, sadly his Wordpress broke my HTML text (just like mine will probably do it too), so I am posting it here.

It uses a weirdness like $named->default_var($default_var), but it works :) For PHP5 only... with previous versions of PHP, every parameter would need to have its own function inside the named classes. Doable, but definitively undesirable :)

PHP:
  1. class named
  2. {
  3.     public function clean()
  4.     {
  5.         foreach ($this as $key => $value) {
  6.             unset($this->$key);
  7.         }
  8.     }
  9.  
  10.     private function __call($method, $arguments)
  11.     {
  12.         if (isset($this->$method)) {
  13.             return $this->$method;
  14.         } else {
  15.             return $arguments[0];
  16.         }
  17.     }
  18. }
  19.  
  20. $named = new named();
  21.  
  22. function inputText($name, $maxlength = 5)
  23. {
  24.     global $named;
  25.  
  26.     $html = "<input type=\"text\" name=\"$name\" ";
  27.     $html .= "maxlength=\"" . $named->maxlength($maxlength) . "\" ";
  28.  
  29.     $html .= " />\n";
  30.  
  31.     $named->clean();
  32.  
  33.     return $html;
  34. }
  35.  
  36. echo inputText('input_name');
  37.  
  38. echo inputText('input_name', $named->maxlength = 10);
  39.  
  40. /*
  41. Outputs:
  42. <input type="text" name="input_name" maxlength="5"  />
  43. <input type="text" name="input_name" maxlength="10"  />
  44. */

But Adam also said that passing named parameters with "=" is easier (and I agree). On my third cup of coffe, this is what I wrote:

PHP:
  1. class named
  2. {
  3.     private $x = array();
  4.  
  5.     public function clean()
  6.     {
  7.         foreach ($this as $key => $value) {
  8.             unset($this->$key);
  9.         }
  10.     }
  11.  
  12.     private function __set($nm, $val)
  13.    {
  14.        $this->x[$nm] = $val;
  15.    }
  16.  
  17.     private function __get($nm)
  18.     {
  19.         global $$nm;
  20.  
  21.         if (isset($$nm)) {
  22.             return $$nm;
  23.         } else {
  24.             return $this->x[$nm];
  25.         }
  26.     }
  27. }
  28.  
  29. class form extends named
  30. {
  31.     function inputText($name)
  32.     {
  33.         $this->maxlength = 5;
  34.         $this->size = 10;
  35.  
  36.         $html = "<input type=\"text\" name=\"$name\" ";
  37.         $html .= "maxlength=\"" . $this->maxlength . "\" ";
  38.         $html .= "size=\"" . $this->size . "\" ";
  39.  
  40.         $html .= " />\n";
  41.  
  42.         $this->clean();
  43.  
  44.         return $html;
  45.     }
  46. }
  47.  
  48. $form = new form();
  49.  
  50. echo $form->inputText('input_name');
  51.  
  52. echo $form->inputText('input_name', $size = 20);
  53.  
  54. /*
  55. Outputs:
  56. <input type="text" name="input_name" maxlength="5" size="10"  />
  57. <input type="text" name="input_name" maxlength="5" size="20"  />
  58. */

$this->maxlength = 5; and $this->size = 10; are used to set the default function "parameters". They can't go inside the parenthesis because otherwise the global keyword can't access it.

blog comments powered by Disqus

Português flagItaliano flagCoreano flagChinês (simplificado) flagEnglish flagAlemâo flagFrancês flagEspanhol flag
Japonês flagÁrabe flagRusso flagHolandês flagBúlgaro flagTcheco flagCroata flagDinamarquês flag
Finlandês flagHindu flagPolonês flagRomeno flagSueco flagGrego flagNorueguês flag 
By N2H
96 DOLETAS de desconto na hospedagem Dreamhost!
Use o "PROMO CODE" INERCIA. LAMP com 20GB de espaço e 1TB de transferência.

Artigos relacionados

  • No Related Posts

Categories