Pyh.conf’25: a new PHP conference for the Russian-speaking community

Voting

: max(six, six)?
(Example: nine)

The Note You're Voting On

npelov at croler dot net
4 years ago
get_defined_vars() returns all variables - locally defined vars and global vars (well actually only super globals). If you need local vars only - for example you need to get variables from a file - let's say config.php and you don't want a missing value to be replaced with a global defined somewhere else.

<?php

/**
* Filters out global vars from get_defined_vars() in order to get local vars only
* @param array $localVars pass get_defined_vars() here
*
* @return array local vars only
*/
function removeGlobals(array $localVars) {
return
array_diff_key($localVars, $GLOBALS);
}

define('CONFIG_FILE_PATH', '/path/to/config.php');
function
readConfig() {
require
CONFIG_FILE_PATH;
$config = removeGlobals(get_defined_vars());
return
$config;
}

?>

<< Back to user notes page

To Top