On Mon, November 27, 2006 7:57 pm, Kelly Jones wrote:
> If I define a function like this:
>
> function foo ($x, $y, $z) {}
>
> and then call: foo(1,2,"bar");
>
> is there a function I can call inside foo() that yields this hash:
>
> {"x" => 1, "y" => 2, "z" => "bar"}
>
> In other words, gives me the values *and names* of the arguments to
> foo?
>
> func_get_args just yields the values.
>
> Having this function would make writing constuctors easier.
>
> The normal way:
>
> fuction ClassName ($x, $y, $z) {
>    $this->x = $x;
>    $this->y = $y;
>    $this->z = $z;
> }
>
> could be replaced with:
>
> function ClassName ($x, $y, $z) {
>    foreach (magic_function_i_want() as $key=>$val) {
>       $this->$key =$val;
>    }
> }
>
> Obviously, it's more of a savings when you have a large number of
> parameters.

It's probably a Bad Road you are on, but you could also consider the
dirt-simple:

function ClassName($x, $y = NULL, $z = NULL) {
  if (is_null($y) && is_null($z) && is_array($x)){
    foreach($x as $k => $v){
      $this->$k = $v;
    }
  }
}

You may even be able to wrap that up in some fancy OOP thingie that
makes you happy.

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to