Page 1 of 1

Get defined variables.

Posted: Thu Dec 21, 2006 5:31 pm
by daedalus__
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.

Posted: Thu Dec 21, 2006 6:45 pm
by Jenk

Code: Select all

var_dump($GLOBALS);
but it'll be nasty because of recursion, so use a foreach.

Posted: Thu Dec 21, 2006 10:22 pm
by hawleyjr

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"; 
    }
Edit: removed my return true; :)

Posted: Fri Dec 22, 2006 3:12 am
by Jenk
Am I missing something? That function will just return true and the rest is superfluous?

Posted: Fri Dec 22, 2006 3:22 am
by Luke
looks very much like that to me too man. :? hawleyjr, explain yourself!

Posted: Fri Dec 22, 2006 3:28 am
by Chris Corbyn
Yeah wtf? Lol.... :lol:

There's recursion in there too.... but it'll never execute.

Posted: Fri Dec 22, 2006 8:31 am
by hawleyjr
Jenk wrote:Am I missing something? That function will just return true and the rest is superfluous?
LOL, that's what I get when I post w/o testing first ;)

I removed the return TRUE; ;)

Posted: Fri Dec 22, 2006 3:34 pm
by daedalus__
Here is what I have been trying to accomplish. Thanks for the help and feel free to make suggestions!

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

)

Posted: Fri Dec 22, 2006 5:44 pm
by Ollie Saunders
Its very sexist but I love it!
Thanks for the help and feel free to make suggestions!
OK this

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))
runs faster as this

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])) {

Posted: Fri Dec 22, 2006 7:22 pm
by daedalus__
I'll do one about men next time, rofl.

Thanks for the suggestion, I would have never thought to code it that way.