Page 1 of 1
Variable-Scope Issues [Solved-ish]
Posted: Tue Jul 27, 2010 7:45 pm
by raptor354
Say, for example, I have code like this...
Code: Select all
$helloworld = "Hello World!";
class Display
{
function HelloWorld()
{
echo $helloworld;
}
}
$display = new Display();
$display->HelloWorld();
To the untrained eye, that looks perfectly fine. But, I can't get anything to happen.
Ok... Plan B: declare them to be global...
Code: Select all
$helloworld = "Hello World!";
class Display
{
function __construct()
{
global $helloworld;
}
function HelloWorld()
{
echo $helloworld;
}
}
$display = new Display();
$display->HelloWorld();
Again... nothing. Shouldn't forcing the variables to be global in the constructor make everything all better?
Ok, last attempt. I put the global declaration inside the function:
Code: Select all
$helloworld = "Hello World!";
class Display
{
function HelloWorld()
{
global $helloworld;
echo $helloworld;
}
}
$display = new Display();
$display->HelloWorld();
Finally some results. But I don't want to have to declare the variables in each of my functions. Is there a way to use global variables throughout a class without using $GLOBALS[]?
Re: Variable-Scope Issues
Posted: Tue Jul 27, 2010 7:53 pm
by buckit
sure:
Code: Select all
$helloworld = "Hello World!";
class Display
{
function HelloWorld($var)
{
echo $var;
}
}
$display = new Display();
$display->HelloWorld($helloworld);
but thats probably not the answer you wanted

Re: Variable-Scope Issues
Posted: Tue Jul 27, 2010 7:57 pm
by raptor354
buckit wrote:sure:
Code: Select all
$helloworld = "Hello World!";
class Display
{
function HelloWorld($var)
{
echo $var;
}
}
$display = new Display();
$display->HelloWorld($helloworld);
but thats probably not the answer you wanted

Haha thanks Buckit, and you're right: that's not quite what I wanted. I'm hoping for a nearly-transparent way of using $helloworld.
Re: Variable-Scope Issues
Posted: Tue Jul 27, 2010 7:59 pm
by buckit
looks like this should work
extract($globals)
untested... found it in an example on php.net
http://php.net/manual/en/language.variables.scope.php
Re: Variable-Scope Issues
Posted: Tue Jul 27, 2010 8:11 pm
by raptor354
Well, it works, but I could do the same thing with:
Maybe my original idea won't bend around the PHP variable scope system. Basically, I want to use a variable that's outside the function as if it's inside the function. I don't think there's a way to do that, other than to define it in every function.
So, here's what I've come up with:
Code: Select all
$helloworld = "Hello World!";
class Display
{
function __construct()
{
global $helloworld;
$this->helloworld = $helloworld;
}
function HelloWorld()
{
echo $this->helloworld;
}
}
$display = new Display();
$display->HelloWorld();
So now, instead of using $helloworld in my function, I'd just have to use $this->helloworld. I guess that'll work.
Re: Variable-Scope Issues
Posted: Tue Jul 27, 2010 8:17 pm
by buckit
raptor354 wrote:
Well, it works, but I could do the same thing with:
.
not really the same.
say you have 2 variables, $a=1, $b=2
your way you could have to call
global $a;
global $b;
right?
the way I posted you could just have to put
extract($globals);
after doing that you can call $a and $b inside your function with no issue.
Re: Variable-Scope Issues
Posted: Tue Jul 27, 2010 8:23 pm
by raptor354
buckit wrote:
not really the same.
say you have 2 variables, $a=1, $b=2
your way you could have to call
global $a;
global $b;
right?
the way I posted you could just have to put
extract($globals);
after doing that you can call $a and $b inside your function with no issue.
Ok, you're right. Right now, however, I only have one global variable that I include in each class. Later on, I'm sure I'll have more, so I might as well switch to the extract() method now. Thanks for the input.