RE: [PHP] func_get_args as a hash? (faster constructors)



Well, I would reply that this is probably as easy to read ...
You should think about poeple having digg into your code, or about you in a few month...

Moreover, having much parameters (more than 4) is not a good idea (for the same reading and understanding reasons). Functions having more than 4 params should probably be refactored... Especially inside the constructor !

this is my own thinking, of course

Vincent


-----Original Message-----
From: Kelly Jones [mailto:kelly.terry.jones@xxxxxxxxx]
Sent: Tue 28/11/2006 2:57
To: php-general@xxxxxxxxxxxxx
Subject: [PHP] func_get_args as a hash? (faster constructors)

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.

--
We're just a Bunch Of Regular Guys, a collective group that's trying
to understand and assimilate technology. We feel that resistance to
new ideas and technology is unwise and ultimately futile.

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



Relevant Pages

  • func_get_args as a hash? (faster constructors)
    ... is there a function I can call inside foothat yields this hash: ... gives me the values *and names* of the arguments to foo? ... func_get_args just yields the values. ... to understand and assimilate technology. ...
    (php.general)
  • Re: [PHP] func_get_args as a hash? (faster constructors)
    ... is there a function I can call inside foothat yields this hash: ... gives me the values *and names* of the arguments to foo? ... func_get_args just yields the values. ... of the args. ...
    (php.general)
  • Re: [PHP] func_get_args as a hash? (faster constructors)
    ... On Mon, November 27, 2006 7:57 pm, Kelly Jones wrote: ... is there a function I can call inside foo() that yields this hash: ... func_get_args just yields the values. ...
    (php.general)
  • Re: Better approach to extension-specific objects...
    ... set foo [openfoo] ... For many years extension writers have used the same inferior approach to ... that has the string representation of a previous object then bad things ... The principle behind Tcl handles is that you can avoid lookup in a hash ...
    (comp.lang.tcl)
  • Re: same function with difference return type
    ... > sometimes scalar, sometimes array? ... > foo() - return hash ... A rock pile ceases to be a rock pile the moment a single man ...
    (comp.lang.perl.misc)