Scope? No scope!



  • Exactly what kind of %^$! retarded scoping rules does $PHP have to make the output of this "heh" only, instead of "hehgnork" like in any SANE language?


    $bloop = 'heh';

    function foo() {
    $bloop .= 'gnork';
    }

    foo();

    echo $bloop;



  • I had to look at this.  I don't know PHP, but I had always thought of it as a 'poor-mans' perl.  I thought you had to mean the question the other way, and re-read it.  PHP explains this as functions having a different scope that's external to the global scope.

    I explain it as someone was drunk when they designed the language.  I guess it does have a protect-the-programmer-from-himself consequence that you can't modify a global without passing it in, but I can't think of any other reason for designing it that way.



  • @dhromed said:

    Exactly what kind of %^$! retarded scoping rules does $PHP have to make the output of this "heh" only, instead of "hehgnork" like in any SANE language?

    I don't know PHP, so I'm just guessing. Feel free to bash me if I'm wrong, but this is what I think:

    A lot of stuff is global in PHP, and there is no way to explicitely declare a local variable within a function. So they had to do it this way, to keep library functions, which need some vars as well and can't guess which variable names are already used for global variables, from unintentionally modifing global variables.



  • @TheDauthi said:

    I don't know PHP, but I had always thought of it as a 'poor-mans' perl.

    Isn't that a bit harsh against Perl? I like to think of PHP as some kind of bloated MS-DOS batch processor for webservers.

    @TheDauthi said:

    I explain it as someone was drunk when they designed the language.  I guess it does have a protect-the-programmer-from-himself consequence that you can't modify a global without passing it in, but I can't think of any other reason for designing it that way.

    Actually, there is no trace of any language design in PHP. I guess this weird scoping was just the easiest implementation.

    Except for (very) simple tasks, don't use PHP. When you find yourself writing functions and maybe use stuff like insert or require, better do it in a "real" language. I've written a simple CMS for a website in PHP some time ago and it was horrible. (Yes, I feel a bit ashamed about having done this. But at least now I can rant about it with some expertise. ;)



  • @Radiation Dude said:

    Actually, there is no trace of any language design in PHP.



    This is not true! There are many traces of language design in PHP! Traces of Perl, traces of C, traces of shellscripting etc. Probably even traces of Cobol if you search hard enough.



  • @Radiation Dude said:

    @TheDauthi said:
    I don't know PHP, but I had always thought of it as a 'poor-mans' perl.

    Isn't that a bit harsh against Perl? I like to think of PHP as some kind of bloated MS-DOS batch processor for webservers.

    Yes.  Please note the use of the word 'had'.  Perl's probably my favored language, actually, and I wouldn't want this kind of WTF above associated with it.



  • <font face="Verdana" size="2">It's because you must declare $bloop as global :

    </font>

    <font face="Verdana" size="2">$bloop = 'heh';

    </font>

    <font face="Verdana" size="2">function foo()</font>
    <font face="Verdana" size="2">{</font>
    <font face="Verdana" size="2">  global $bloop;</font>
    <font face="Verdana" size="2">  $bloop .= 'gnork';</font>
    <font face="Verdana" size="2">}</font>

    <font face="Verdana" size="2">foo();</font>

    <font face="Verdana" size="2">echo $bloop;</font>

    <font face="Verdana" size="2">
    Yes, that's fucked up. But the worse about php scope is actually code like this :

    if(true)
    {
       $bla = 34;
    }
    echo $bla;
    </font>



  • @Silex said:

    <font face="Verdana" size="2">
    Yes, that's fucked up. But the worse about php scope is actually code like this :

    if(true)
    {
       $bla = 34;
    }
    echo $bla;
    </font>


    Prints 34 for me - what's wrong with that?



  • @TDC said:

    @Silex said:
    <font face="Verdana" size="2">
    Yes, that's fucked up. But the worse about php scope is actually code like this :

    if(true)
    {
       $bla = 34;
    }
    echo $bla;
    </font>


    Prints 34 for me - what's wrong with that?


    Absolutely nothing wrong with that.

    However, the C family of languages (bitchslap me if I'm wrong) tend to create a new sub-scope for each and every {} block, including ifs and loops, which is likely what Silex is used to. Which is just as retarded as $PHP's fully absent scoping. There is such a thing as too much scope, I think.

    JScript/Javascript/ECMA creates a new sub scope only inside functions. [b]var[/b]-declared variables originate in the scope they're declared in, and any call to a variable that isn't present in the current scope will first be located in a higher scope, etc., until no more scopes are left, in which case the var becomes declared at run-time as a global var.

    An approach that is both usable and predictable, except for the implicit var declaration. I never make use of implicits and always var my vars before using them. A global var shall be varred at the global scope. It's better that way.

    $PHP's scoping WTF stems from the fact that you can't explicitly declare a variable. Both calls and creations use that pointless $-syntax. It wouldn't be so bad if declaration used the $ and usage used just the var's name. That would disable magic quotes and variable varnames, but hey, no loss there!

    PS.
    Silex, the [b]global $bloop;[/b] bit did the trick. Thanks!



  • PHP's system is predictable, too. There's actually only one rule:

    When being in a function, per default the scope is that of the function.

    This even applies to the pseudo-oo-features of PHP:


    class foo{

     private $bar = 1;

     function increment(){

      $bar++; //This won't affect our member

      $this->bar++; //This will

     }

    }

    It might be a WTF, but at least it's consistent ;)


    But yes, it's not really intuitive and probably leads to many bugs. Although unwanted automatic casts probably produce even more.



  • @dhromed said:


    However, the C family of languages (bitchslap me if I'm wrong) tend to create a new sub-scope for each and every {} block, including ifs and loops, which is likely what Silex is used to.


    Yes, that's what I'm used to. It's the same in Java, C#, C/C++... and I think it's what is sane. Of course for a script language I can understand they allow it, still I dislike it.



  • @Ozru said:

    You can dislike it all you want, but because variables aren't explicitly declared, there's no way to have the assignment in the braces differentiate between modifying an existing variable and creating a new one. Whichever choice you make will tick some people off. The choice they made is the one that allows more functionality, even if it ticks you off that the implicit declaration is outside the brace scope instead of inside it.


    Good point.



  • @dhromed said:

    Exactly what kind of %^$! retarded scoping rules does $PHP have to make the output of this "heh" only, instead of "hehgnork" like in any SANE language?


    $bloop = 'heh';

    function foo() {
    $bloop .= 'gnork';
    }

    foo();

    echo $bloop;

    Since when does PHP have a scopes at all?

    Now other languages with dynamic scopes (closures) will also display this behavior (JS comes to mind, Ruby also does it when you're using blocks, not when you're using methods, Python doesn't because Python doesn't have full read/write closures)

    @TheDauthi said:
    I explain it as someone was drunk when they designed the language.  I guess it does have a protect-the-programmer-from-himself consequence that you can't modify a global without passing it in, but I can't think of any other reason for designing it that way.

    PHP hasn't been designed in the first place, it grew from a bunch of perl macros which was mutated into something that looks like a language on which additional features were anarchically bolted.



  • For future reference: http://www.php.net/variables.scope


Log in to reply