Globalizing vars in class methods doesnt seem to work, var disappears after global, var is inaccessible in other methods/funcs [message #178789] |
Fri, 03 August 2012 17:45 |
J. Frank Parnell
Messages: 12 Registered: January 2012
Karma:
|
Junior Member |
|
|
<?php
class test{
function __construct(){
$this->makevar();
global $var;
echo '<hr>__const(): '.$var;
}
function makevar(){
$var = 'something';
echo '<hr>just declared in makevar(): '.$var;
global $var;
echo '<hr>after global in makevar(): '.$var;
}
}//class
$t = new test;
echo '<hr>outside class: '.$var;
global $var;
echo '<hr>outside class after global: '.$var;
?>
this outputs:
just declared in makevar(): something
after global in makevar(): [nothing]
__const():[nothing]
outside class: [nothing]
outside class after global: [nothing]
So, 1, why does global $var in the makevar() make it null?
and B, why dont I have any access to $var in the __constructor or outside the class?
In the real script, $var will be an instance of a different class that I want to use all over the place.
thanks, J
|
|
|