Ryan Hubbard wrote:
Could someone provide me with some code to hold an object reference in
a static variable in a function.
function a(&$t){
static $b;
if(is_object($b)){
print "IN - $b->logfile";
} else {
print "Setting<BR>";
$b = &$t;
print "IN - $b->logfile<BR>";
}
}
calling a with a object reference then should hold its reference and
if called with no args it should print out the references variable.
But when called with no aguments the static varible b is null. Can
some one help be out here. Thanks
$text->logfile = "TEST";
a(&$test);
a();
output
------
Setting
IN - TEST
Setting
IN - TEST
Calling a() without any parameters causes PHP to issue a warning because
you have defined a required parameter for the function "a". When you
declare a variable in a function to be static, it is only static for the
life of the function. Once the function exits, all values associated
with it are marked for deletion. Try the following modification to your
code and you should see the difference.
<?php
static $b;
function a(&$t){
if(is_object($GLOBALS['b'])){
print "IN - " . $GLOBALS['b']->logfile . "\n";
} else {
print "Setting\n";
$GLOBALS['b'] =& $t;
print "IN - " . $GLOBALS['b']->logfile . "\n";
}
}
class test {
var $logfile;
}
$test = new test();
$test->logfile = "TEST";
a($test);
a($test);
?>
output
-----------
Setting
IN - TEST
IN - TEST
--
Amir Khawaja.
----------------------------------
Rules are written for those who lack the ability to truly reason, But
for those who can, the rules become nothing more than guidelines, And
live their lives governed not by rules but by reason.
- James McGuigan