Created
July 11, 2010 08:58
-
-
Save gnrfan/471412 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
array(2) { | |
["args"]=> | |
array(1) { | |
[0]=> | |
int(1976) | |
} | |
["kwargs"]=> | |
array(2) { | |
["age"]=> | |
int(33) | |
["name"]=> | |
string(7) "Antonio" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.