note 96971 deleted from language.variables.scope by danbrown

From: Date: Thu, 25 Mar 2010 15:48:39 +0000
Subject: note 96971 deleted from language.variables.scope by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-167394@lists.php.net to get a copy of this message
Note Submitter: nekroshorume at gmail dot com Reason: 2 ---- wanna see a little dirty trick using the $GLOBALS ? <?php definePropertiesStart($my_array); $name = $user = fabio; $age = 17; $city = php_land; $gender = male; $id = 265649; definePropertiesEnd(); print_r($my_array); //outputs: Array ( [user] => fabio [name] => fabio [age] => 17 [city] => php_land [gender] => male [id] => 265649 ) ?> of course "definePropertiesStart" and "definePropertiesEnd" are functions I developed myself and here they come: <?php function definePropertiesStart(&$var){ // ok, first lets find the name of the variable you are using to hold the associations $ret = ''; $tmp = $var; $var = md5(uniqid(rand(), TRUE)); $key = array_keys($GLOBALS); foreach ( $key as $k ) if ( $GLOBALS[$k] === $var ) { $ret = $k; break; } $var = $tmp; // now store some information about the current status of things in GLOBALS $GLOBALS['property_var'] = $ret; // variable name $GLOBALS['property_start'] = count($GLOBALS)+1; // offset of variables so far } function definePropertiesEnd(){ // get holding variable name $varname = $GLOBALS['property_var']; // pass association to the holding variable $GLOBALS[$varname] = array_slice($GLOBALS,$GLOBALS['property_start']); } ?>

« previous php.notes (#167394) next »