Get defined variables.
Posted: Thu Dec 21, 2006 5:31 pm
I'm trying to get a list of all user-defined variables. Is this possible? I tried to use get_defined_vars() but this is inside of a method and it stays inside the scope of the class.
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
var_dump($GLOBALS);Code: Select all
//**************************************
//
// Name: Show variables & values
// Description:This code show variables
// and values list. use dd("GLOBALS") to sh
// ow program state in browser
// By: PHP Code Exchange
//**************************************
//
/*
This code show all variables with values in browser window.
use: dd("GLOBALS");
showAllVariables("GLOBALS");
Sample of output:
http://www.planet-source-code.com/vb/sc ... 6&lngWId=8
*/
function showAllVariables($v) {
global $a;
echo "<blockquote>\n";
$q= "while(list(\$key,\$val) = each($".$v. ") ) { ".
" echo \"<b>\$key</b>=>\$val.<br>\";".
" if(( is_array(\$val)) && (\$key != \"GLOBALS\")) {".
" @showAllVariables( \$v.\"[\".\$key.\"]\" );".
"}}";
eval($q);
echo "</blockquote>\n";
}LOL, that's what I get when I post w/o testing firstJenk wrote:Am I missing something? That function will just return true and the rest is superfluous?
Code: Select all
function findInstance($class)
{
foreach ($GLOBALS as $key => $value)
{
$invalid = array('_ENV', '_POST', '_GET', '_COOKIE', '_SERVER', 'HTTP_ENV_VARS', 'HTTP_POST_VARS', 'HTTP_GET_VARS', 'HTTP_COOKIE_VARS', 'HTTP_SERVER_VARS', 'HTTP_POST_FILES');
if (! in_array($key, $invalid))
{
if (is_a($value, $class))
{
return $value;
}
}
}
}
class girls
{
public $ugly;
protected $stupid;
private $hot;
public function __construct()
{
$this->ugly = ' looks similar to a frog-buffalo mutant.<br />';
$this->stupid = ' gives new meaning to the word.<br />';
$this->hot = ' is hot but what a smurf<br />';
}
public function go()
{
echo $this->ugly;
echo $this->stupid;
echo $this->hot;
}
}
$chicken = 'yum';
$beef = 'mmm';
$fish = 'ooooh';
$girls = new girls();
$result = findInstance('girls');
echo '<pre>';
print_r($result);
echo '<pre>';result wrote: girls Object
(
[ugly] => looks similar to a frog-buffalo mutant.
[stupid:protected] => gives new meaning to the word.
[hot:private] => is hot but what a smurf
)
OK thisThanks for the help and feel free to make suggestions!
Code: Select all
$invalid = array('_ENV', '_POST', '_GET', '_COOKIE', '_SERVER', 'HTTP_ENV_VARS', 'HTTP_POST_VARS', 'HTTP_GET_VARS', 'HTTP_COOKIE_VARS', 'HTTP_SERVER_VARS', 'HTTP_POST_FILES');
if (! in_array($key, $invalid))Code: Select all
static $invalid = array('_ENV' => true, '_POST' => true, '_GET' => true, '_COOKIE' => true, '_SERVER' => true, 'HTTP_ENV_VARS' => true, 'HTTP_POST_VARS' => true, 'HTTP_GET_VARS' => true, 'HTTP_COOKIE_VARS' => true, 'HTTP_SERVER_VARS' => true, 'HTTP_POST_FILES');
if (isset($invalid[$key])) {