Variable-Scope Issues [Solved-ish]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
raptor354
Forum Newbie
Posts: 13
Joined: Tue Jun 16, 2009 3:21 pm

Variable-Scope Issues [Solved-ish]

Post 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[]?
Last edited by raptor354 on Tue Jul 27, 2010 8:16 pm, edited 1 time in total.
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

Re: Variable-Scope Issues

Post 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 :wink:
raptor354
Forum Newbie
Posts: 13
Joined: Tue Jun 16, 2009 3:21 pm

Re: Variable-Scope Issues

Post 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 :wink:
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.
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

Re: Variable-Scope Issues

Post 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
raptor354
Forum Newbie
Posts: 13
Joined: Tue Jun 16, 2009 3:21 pm

Re: Variable-Scope Issues

Post by raptor354 »

buckit wrote: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
Well, it works, but I could do the same thing with:

Code: Select all

global $helloworld;
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.
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

Re: Variable-Scope Issues

Post by buckit »

raptor354 wrote: Well, it works, but I could do the same thing with:

Code: Select all

global $helloworld;
.

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.
raptor354
Forum Newbie
Posts: 13
Joined: Tue Jun 16, 2009 3:21 pm

Re: Variable-Scope Issues

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