Skip to content

Instantly share code, notes, and snippets.

@gnrfan
Created July 11, 2010 08:58
Show Gist options
  • Save gnrfan/471412 to your computer and use it in GitHub Desktop.
Save gnrfan/471412 to your computer and use it in GitHub Desktop.
<?php
function npparse($values) {
$result = array();
$result['args'] = array();
$result['kwargs'] = array();
$key = NULL;
foreach($values as $v) {
if (!is_null($key)) {
$result['kwargs'][$key] = $v;
$key = NULL;
continue;
}
if (is_string($v)) {
if ($v[0] == ':') {
$key = substr($v, 1);
continue;
}
}
$result['args'][] = $v;
}
return $result;
}
function test() {
var_dump(npparse(func_get_args()));
}
test(":age", 33, ":name", "Antonio", 1976);
?>
array(2) {
["args"]=>
array(1) {
[0]=>
int(1976)
}
["kwargs"]=>
array(2) {
["age"]=>
int(33)
["name"]=>
string(7) "Antonio"
}
}
@gnrfan
Copy link
Author

gnrfan commented Jul 11, 2010

Came up with this named parameter emulation hack for PHP that I swear I haven´t seen before :) It's a pretty simple idea but at least looks like a neat hack to me :) I got inspired by Python's _args and *_kwargs and Ruby's symbols.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment