Get defined variables.

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Get defined variables.

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

var_dump($GLOBALS);
but it'll be nasty because of recursion, so use a foreach.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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; :)
Last edited by hawleyjr on Fri Dec 22, 2006 8:30 am, edited 1 time in total.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Am I missing something? That function will just return true and the rest is superfluous?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

looks very much like that to me too man. :? hawleyjr, explain yourself!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Yeah wtf? Lol.... :lol:

There's recursion in there too.... but it'll never execute.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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; ;)
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post 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

)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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])) {
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post 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.
Post Reply