well yeah..
Code: Select all
<?php
class test {
public function c() {
// we got an error in this function so we want to debug it!
global $var1;
global $var2;
global $var4;
global $var5;
global $var8;
global $var9;
global $var11;
global $var13;
//.......code
print_r(get_defined_vars());
}
}
$tester = new test;
$tester->c();
?>
or
Code: Select all
<?php
class test {
public function a($var1, $var5, $var8, $var11, $var14) {
}
public function b($var1, $var2, $var4, $var5, $var8, $var9, $var11, $var13) {
}
public function c($var1, $var2, $var4, $var5, $var8, $var9, $var11, $var13) {
print_r(get_defined_vars());
}
}
$tester = new test;
$tester->c($var1, $var2, $var4, $var5, $var8, $var9, $var11, $var13);
?>
or
Code: Select all
<?php
/* define our registry and pass variables to it by reference */
$registry = array();
$registry['var1'] = &$var1;
$registry['var2'] = &$var2;
$registry['var3'] = &$var3;
$registry['var4'] = &$var4;
$registry['var5'] = &$var5;
$registry['var6'] = &$var6;
$registry['var7'] = &$var7;
$registry['var8'] = &$var8;
$registry['var9'] = &$var9;
$registry['var10'] = &$var10;
$registry['var11'] = &$var11;
$registry['var12'] = &$var12;
$registry['var13'] = &$var13;
$registry['var14'] = &$var14;
$registry['var15'] = &$var15;
$registry['var16'] = &$var16;
class test {
public function a(&$registry) {
}
public function b(&$registry) {
}
public function c(&$registry) {
print_r(get_defined_vars());
}
}
$tester = new test;
$tester->c($registry);
?>
or
Code: Select all
<?php
/* define our registry and pass variables to it by reference */
$registry = array();
$registry['var1'] = &$var1;
$registry['var2'] = &$var2;
$registry['var3'] = &$var3;
$registry['var4'] = &$var4;
$registry['var5'] = &$var5;
$registry['var6'] = &$var6;
$registry['var7'] = &$var7;
$registry['var8'] = &$var8;
$registry['var9'] = &$var9;
$registry['var10'] = &$var10;
$registry['var11'] = &$var11;
$registry['var12'] = &$var12;
$registry['var13'] = &$var13;
$registry['var14'] = &$var14;
$registry['var15'] = &$var15;
$registry['var16'] = &$var16;
class test {
public function a() {
global $registry;
extract($registry);
}
public function b() {
global $registry;
extract($registry);
}
public function c() {
global $registry;
extract($registry);
print_r(get_defined_vars());
}
}
$tester = new test;
$tester->c();
?>
or my method..
Code: Select all
<?php
/* define our registry and pass variables to it by reference */
$registry = new registry(get_defined_vars());
class test {
public function a() {
extract(registry());
}
public function b() {
extract(registry());
}
public function c() {
extract(registry());
print_r(get_defined_vars());
}
}
$tester = new test;
$tester->c();
?>
I know from coding everyday which one uses the least lines, is easiest to use, and never needs updated no matter how many new variables you need access too from all levels.. the only other thing that comes close to it (in my opinion) is:
Code: Select all
<?php
class test {
public function c() {
extract($GLOBALS);
print_r(get_defined_vars());
}
}
$tester = new test;
$tester->c();
?>
however if you have to debug with the print_r(get_defined_vars()); you'll soon see which one gives you the shortest tidiest print_r results - and purely to prove how much of a matter of personal preference this is.. in mine it unset($GLOBALS) - which means there are no globals available anymore (i know the technicalities involved and that's not strictly true.. however you can't $GLOBALS['var'] as $GLOBALS is undefined

*joy*
also as if to compound my sadness there's just something that I like about saving variables by reference in a static variable within a self saving object *shrugs*