PHP and Python Function Arguments

Categories: blog



While doing some work, I tried to find about function arguments in PHP. I still prefer python ways of doing things

PYTHON:
  1. def test(tmp1, tmp2)
  2.   print tmp1
  3.   print tmp2
  4.  
  5. test(tmp2='woot2', tmp1='woot1')
  6.  
  7. >>woot1
  8. >>woot2

 

Thats a nice feature that I wished PHP had.  So that I dont have to remember the order of arguments and just type in the "key" and its "values". Its kinda natural that way.

(While I was trying to find a nice syntax highlighting plugins for WP, I tried iG:Syntax Hiliter, works like a charm.)

The best way I can do for php is...

PHP:
  1. textfield(array('name' => 'username', 'size' => 30));
  2.  
  3. // tried using the python way..
  4. test($tmp2='woot2', $tmp1='woot1');
  5.  
  6. woot2
  7. woot1

func_get_args() just doesnt cut it.

I traveled this path when I wanted to differentiate between list and associative array. Which leads me to a drupal issues page. Differentiate between associative and numeric php arrays.

*Apparently there's some issues regarding html formatting of symbols > (greater than) which bothers me. It doesnt bother me enough to fix it at this moment. Ah.. priorities.. priorities.


I experimented a little. This is the best that I can do for the moment..

PHP:
  1. $default_value = array(
  2.                         'name'   => '',
  3.                         'size'   => '25',
  4.                         'default'   => '',
  5.                         'id'        => '',
  6.                         'extra'  => '',
  7.                         'desc'    => ''
  8.                     );
  9.     if(func_num_args() == 1)
  10.     {
  11.         if(is_assoc($newattr))
  12.         {
  13.             foreach($default_value as $attr_name => $attr_value)
  14.             {
  15.                 $$attr_name = (array_key_exists($attr_name, $newattr)) ? $newattr[$attr_name]:
  16.                               ((array_key_exists($attr_name, $default_value)) ?
  17.                               $default_value[$attr_name]:'');
  18.             }
  19.         }
  20.         else
  21.         {
  22.             $tmp_key = '';
  23.             $tmp_key = key($default_value);
  24.             $$tmp_key = $newattr;
  25.             foreach($default_value as $attr_name => $attr_value)
  26.             {
  27.                 if(!isset($$attr_name))
  28.                 {
  29.                     $$attr_name = $attr_value;
  30.                 }
  31.             }
  32.         }
  33.     }
  34.     else
  35.     {
  36.         if(func_num_args()> count($default_value))
  37.         {
  38.             die('error! Too many arguments, giving me a headache!');
  39.             exit();
  40.         }
  41.         $args = func_get_args();
  42.         $tmp_key = '';
  43.         for($i=0; $i<count($args); $tmp_key="key($default_value);" $$tmp_key="$args[$i];" //$$attr[$i="$newattr[$i];"> $attr_value)
  44.         {
  45.             if(!isset($$attr_name))
  46.             {
  47.                 $$attr_name = $attr_value;
  48.             }
  49.         }
  50.     }

No Comments yet »

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by WordPress with GimpStyle Theme design by Horacio Bella.
Entries and comments feeds. Valid XHTML and CSS.