开发者

PHP array argument

Is there a way to make the arguments of a function act as an array? I'm finding this difficult to开发者_如何学JAVA explain.

Here's kind of an example.. When you declare an array, you can define the keys => values like so:

$array = array(
    "key" => "value",
    "other_key" => "other_value"
);

And if I make a function that for an example outputs these onto the document, I could have:

function write($ar)
{
    foreach($ar as $key => $value)
        echo "$key: $value<br />";
}

write($array); // parse previously mentioned array

What I want to be able to do is omit the need to parse an array like above or below examples..

write(array(
    "key" => "value",
    "other_key" => "other_value"
));

I know I can use func_get_args() to list any amount of arguments, but is there a similar function that lets you parse key => value pairs rather than just a list of values?

Hope I described this in a way that makes sense, what I essentially want to end up with is something like:

write(
    "key" => "value",
    "other_key" => "other_value"
);


A function cannot take the argument as an array structure, so your best bet would be to use the method you specified in your second to last example:

write(array(
    "key" => "value",
    "other_key" => "other_value"
));

You could then have a default array within your function (if desired) so you could merge the two together so you always have a decent set of data.

EDIT

Unless you want to go crazy and pass it through as a string:

write('
    "key" => "value",
    "other_key" => "other_value"
');

And then parse that out on the other side... but IMO I wouldn't bother, potentially opening yourself up to issues here.


I know I can use func_get_args() to list any amount of arguments, but is there a similar function that lets you parse key => value pairs rather than just a list of values?

No, that's not possible in PHP. However you can fork PHP and implement such a syntax or make a code preprocessor ;)


well, I assume that you can send an object instead of an array and declare like that.

    //the class
    class CustomType{
       public $key1;
       public $key2;
    }

    //the function 
    function write(CustomType $customType){

    }

//call the funcion write
$a = new CustomType();
$a->key1 = 1;
$a->key2 = 2;
write($a);

the above will force the user to send a custom object. it is better than arrays, but then again, the user can ignore and not set $key2.

BTW, if you choose this method, you might as well do it in JAVA style with getters and setters. like here: http://www.abbeyworkshop.com/howto/java/salesTaxBean/index.html

that is as good as it gets since php is typeless, but i think it is much better then arrays

Edit One of the bad usage in PHP is using arrays as params. Almost every framework does that and you can find your self diving into sources in order to understand the array structure.

When you declare a class, you get it as autocomplete in your editor, and if not you just need to open the class to understand its structure. I believe this is better programming style.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜