Page 1 of 1

I *hate* how PHP variables are scoped

Posted: Fri Jul 06, 2007 12:57 pm
by ReverendDexter
Ok, generally my hate is fueled by a lack of understanding, so maybe some of y'all can explain this.

What I'm used to is variables being accessable from where they're declared and everything narrower than that, i.e. along the following psuedocode:

Code: Select all

int foo;

function display_foo()
{
    echo foo;
}
should work, as the function is narrower scope than where foo was declared.

As should the following:

Code: Select all

class bar
{
    private $foo = 50;

    public function get_foo()
   {
        return $foo;
   }
}
But it doesn't work that way, and it drives me ABSOLUTELY $#@#%ing NUTS. It's totally natural to me that a variable is NOT accessible from a larger scope, i.e

Code: Select all

function herbert()
{
    int taco = 20;
}
echo taco;
should fail miserably, unless there's a taco declared prior in the appropriate scope, and

Code: Select all

int tamale = 100;
function combo_plate
{
    int tamale = 50;
    echo tamale;
}
echo tamale;
should print "50 100" as the locally scoped tamale should displace the less locally scoped tamale while in its declared scope.

So, does what I'm saying make sense to anyone, or at least make enough sense to *not* make sense, so someone can explain to me where I'm wrong?

Thanks!

Posted: Fri Jul 06, 2007 1:19 pm
by vigge89
Use $this when accessing variables in a class:

Code: Select all

class bar
{
    private $foo = 50;

    public function get_foo()
   {
        return $this->foo;
   }
}
Accessing variables outside a function (variable located in the global scope):

Code: Select all

$var = 'text';
function test1() {
    echo $GLOBALS['var'];
}
function test2() {
    global $var;
    echo $var;
}

Posted: Fri Jul 06, 2007 1:21 pm
by stereofrog
php scoping rules may seem weird and limited compared to C-style block-level scoping or java object scoping, however I think it's not a bad idea to limit variable scope as much as possible. Lexical, especially global, variables are often source of errors in C programs. As to object scoping, I agree "$this->" is tedious, it would be nice to have a shortcut (like ruby's @).

Posted: Fri Jul 06, 2007 1:23 pm
by superdezign
I like PHP's scope better than C++'s, as I treated C++ as if it had the same scope. Accessing variables without me explicitly telling it to can cause a lot of unexpected, hard to debug errors.

Posted: Fri Jul 06, 2007 1:32 pm
by Weirdan
PHP takes another approach and to access global scope in the function you need to declare your variables as global:

Code: Select all

$variable = 'something';
function func() {
   global $variable;
   echo $variable . "\n"; // prints 'something'
}

Posted: Fri Jul 06, 2007 2:01 pm
by ReverendDexter
stereofrog wrote:php scoping rules may seem weird and limited compared to C-style block-level scoping or java object scoping, however I think it's not a bad idea to limit variable scope as much as possible. Lexical, especially global, variables are often source of errors in C programs. As to object scoping, I agree "$this->" is tedious, it would be nice to have a shortcut (like ruby's @).
I think you hit the nail on the head - I started on C++, and grew very used to where I could and couldn't access my variables in that language. So having to specify "of course I mean the one I declared... the ONLY $foo in the ENTIRE program", seems redundant. If I want to say "not that $foo, but this $foo instead", that makes sense that I should have declare the narrower-scoped foo.

I guess I'll just have to get used to it.

Posted: Fri Jul 06, 2007 3:18 pm
by Ambush Commander
Because PHP does not require that its variables be declared, it's necessary that variables from the global scope be explicitly declared global to prevent willy-nilly collisions.

Posted: Fri Jul 06, 2007 3:21 pm
by MrPotatoes
ReverendDexter wrote:
stereofrog wrote:php scoping rules may seem weird and limited compared to C-style block-level scoping or java object scoping, however I think it's not a bad idea to limit variable scope as much as possible. Lexical, especially global, variables are often source of errors in C programs. As to object scoping, I agree "$this->" is tedious, it would be nice to have a shortcut (like ruby's @).
I think you hit the nail on the head - I started on C++, and grew very used to where I could and couldn't access my variables in that language. So having to specify "of course I mean the one I declared... the ONLY $foo in the ENTIRE program", seems redundant. If I want to say "not that $foo, but this $foo instead", that makes sense that I should have declare the narrower-scoped foo.

I guess I'll just have to get used to it.
i started with C/C++/Java and i honestly have to say that i prefer the PHP scoping rules. i'm just not a fan of globals at all so i wish there was a better way around that.

Posted: Fri Jul 06, 2007 3:32 pm
by Z3RO21
I like the way PHP handles the scopes. I also do not like globals I prefer using registry style objects with static instances to store information for retrieval in other parts of the application.

Posted: Fri Jul 06, 2007 4:36 pm
by Luke
hey Dex, you can see where I got many of my opinions, huh? :lol: Any of this sound familiar?

Posted: Fri Jul 06, 2007 5:36 pm
by ReverendDexter
I know I'd certainly be happier with some form of block scope; bugs that arrive from overlapping scope can easily be avoided by using descriptive variable names. But, now I at least can understand why PHP is behaving the way it does, and knowing that, I can "drive around it".

That being said, I don't think I'd suggest PHP as a first language for a programmer, I think it encourages some "interesting" habits. ;)