note 96971 added to language.variables.scope
| From: | nekroshorume at gmail dot com | Date: | Thu, 25 Mar 2010 14:14:52 +0000 |
| Subject: | note 96971 added to language.variables.scope | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-167386@lists.php.net to get a copy of this message | ||
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']);
}
?>
----
Server IP: 187.16.23.128
Probable Submitter: 189.74.196.253
----
Manual Page -- http://www.php.net/manual/en/language.variables.scope.php
Edit -- https://master.php.net/note/edit/96971
Del: integrated -- https://master.php.net/note/delete/96971/integrated
Del: useless -- https://master.php.net/note/delete/96971/useless
Del: bad code -- https://master.php.net/note/delete/96971/bad+code
Del: spam -- https://master.php.net/note/delete/96971/spam
Del: non-english -- https://master.php.net/note/delete/96971/non-english
Del: in docs -- https://master.php.net/note/delete/96971/in+docs
Del: other reasons-- https://master.php.net/note/delete/96971
Reject -- https://master.php.net/note/reject/96971
Search -- https://master.php.net/manage/user-notes.php