Variable scope - function in function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul Lautman

    Variable scope - function in function

    Here's something that I can't manage to find explicitly documented. In the
    following snippet:

    function outer() {
    global $a;
    function inner() {
    global $a;
    echo $a.'1<br>';
    }
    $a = 's';
    inner();
    echo $a.'2<br>';

    }
    outer();
    ?>

    If either of the globals statements is removed, the variable is not
    accessable within inner.


  • Chung Leong

    #2
    Re: Variable scope - function in function


    Paul Lautman wrote:[color=blue]
    > Here's something that I can't manage to find explicitly documented. In the
    > following snippet:
    >
    > function outer() {
    > global $a;
    > function inner() {
    > global $a;
    > echo $a.'1<br>';
    > }
    > $a = 's';
    > inner();
    > echo $a.'2<br>';
    >
    > }
    > outer();
    > ?>
    >
    > If either of the globals statements is removed, the variable is not
    > accessable within inner.[/color]

    What you're trying to do won't work if you call outer() more than once.
    PHP has no support for closure or inner functions.

    Comment

    • Paul Lautman

      #3
      Re: Variable scope - function in function

      Chung Leong wrote:[color=blue]
      > Paul Lautman wrote:[color=green]
      >> Here's something that I can't manage to find explicitly documented.
      >> In the following snippet:
      >>
      >> function outer() {
      >> global $a;
      >> function inner() {
      >> global $a;
      >> echo $a.'1<br>';
      >> }
      >> $a = 's';
      >> inner();
      >> echo $a.'2<br>';
      >>
      >> }
      >> outer();[color=darkred]
      >>>[/color]
      >>
      >> If either of the globals statements is removed, the variable is not
      >> accessable within inner.[/color]
      >
      > What you're trying to do won't work if you call outer() more than
      > once. PHP has no support for closure or inner functions.[/color]

      Interesting, that case indeed fails. Yet I came across this problem when
      defining a sort function for usort. In that case, one call call the sort
      function more than once.


      Comment

      • Rik

        #4
        Re: Variable scope - function in function

        Chung Leong wrote:[color=blue]
        > Paul Lautman wrote:[color=green]
        >> Here's something that I can't manage to find explicitly documented.
        >> In the following snippet:
        >>
        >> function outer() {
        >> global $a;
        >> function inner() {
        >> global $a;
        >> echo $a.'1<br>';
        >> }
        >> $a = 's';
        >> inner();
        >> echo $a.'2<br>';
        >>
        >> }
        >> outer();[color=darkred]
        >>>[/color]
        >>
        >> If either of the globals statements is removed, the variable is not
        >> accessable within inner.[/color][/color]

        Which is abolutely logical.
        [color=blue]
        > What you're trying to do won't work if you call outer() more than
        > once.
        > PHP has no support for closure or inner functions.[/color]


        You could circumvent this by:
        if(!function_ex ists('inner'){
        //define function
        }

        I'm very curious though why one would need such an imho messy function
        declaration.

        Grtz,
        --
        Rik Wasmus


        Comment

        • Mladen Gogala

          #5
          Re: Variable scope - function in function

          On Sun, 21 May 2006 21:41:27 +0100, Paul Lautman wrote:
          [color=blue]
          > Chung Leong wrote:[color=green]
          >> Paul Lautman wrote:[color=darkred]
          >>> Here's something that I can't manage to find explicitly documented.
          >>> In the following snippet:
          >>>
          >>> function outer() {
          >>> global $a;
          >>> function inner() {
          >>> global $a;
          >>> echo $a.'1<br>';
          >>> }
          >>> $a = 's';
          >>> inner();
          >>> echo $a.'2<br>';
          >>>
          >>> }
          >>> outer();
          >>>>
          >>>
          >>> If either of the globals statements is removed, the variable is not
          >>> accessable within inner.[/color]
          >>
          >> What you're trying to do won't work if you call outer() more than
          >> once. PHP has no support for closure or inner functions.[/color]
          >
          > Interesting, that case indeed fails. Yet I came across this problem when
          > defining a sort function for usort. In that case, one call call the sort
          > function more than once.[/color]


          Well, it fails complaining about the duplicate declaration, not about the
          invocation. This version will work:

          #!/usr/local/bin/php
          <?php
          $a="A";
          function outer() {
          global $a;
          if ($a=="A") {
          function inner() {
          global $a;
          echo $a."1\n";
          }
          }
          $a = "s";
          inner();
          echo $a."2\n";

          }
          outer();
          outer();
          ?>
          $

          This way, the inner function is not re-declared during the second
          invocation. This can also be simulated by using eval.


          --


          Comment

          • Paul Lautman

            #6
            Re: Variable scope - function in function

            Rik wrote:[color=blue]
            > Chung Leong wrote:[color=green]
            >> Paul Lautman wrote:[color=darkred]
            >>> Here's something that I can't manage to find explicitly documented.
            >>> In the following snippet:
            >>>
            >>> function outer() {
            >>> global $a;
            >>> function inner() {
            >>> global $a;
            >>> echo $a.'1<br>';
            >>> }
            >>> $a = 's';
            >>> inner();
            >>> echo $a.'2<br>';
            >>>
            >>> }
            >>> outer();
            >>>>
            >>>
            >>> If either of the globals statements is removed, the variable is not
            >>> accessable within inner.[/color][/color]
            >
            > Which is abolutely logical.
            >[color=green]
            >> What you're trying to do won't work if you call outer() more than
            >> once.
            >> PHP has no support for closure or inner functions.[/color]
            >
            >
            > You could circumvent this by:
            > if(!function_ex ists('inner'){
            > //define function
            > }
            >
            > I'm very curious though why one would need such an imho messy function
            > declaration.
            >
            > Grtz,[/color]

            I don't actually need it. What happened was that I was having trouble
            getting a variable to be global so that I could use a single callback
            function for my usort and have it sort on different "fields" in my array. It
            worked OK in a test setup, but not in the application. Turns out that I
            hadn't realised that in the application, the callback function was declared
            inside another function, whereas in the test setup it was at the top level.


            Comment

            • Rik

              #7
              Re: Variable scope - function in function

              Paul Lautman wrote:[color=blue][color=green][color=darkred]
              >>> What you're trying to do won't work if you call outer() more than
              >>> once.
              >>> PHP has no support for closure or inner functions.[/color]
              >> You could circumvent this by:
              >> if(!function_ex ists('inner'){
              >> //define function
              >> }
              >>
              >> I'm very curious though why one would need such an imho messy
              >> function declaration.
              >>
              >> Grtz,[/color]
              >
              > I don't actually need it. What happened was that I was having trouble
              > getting a variable to be global so that I could use a single callback
              > function for my usort and have it sort on different "fields" in my
              > array. It worked OK in a test setup, but not in the application.
              > Turns out that I hadn't realised that in the application, the
              > callback function was declared inside another function, whereas in
              > the test setup it was at the top level.[/color]


              Check, while nesting more and more it becomes hard to find errors like that.
              I always try to keep "home-made" funstions in a seperate include for that
              reason.

              Grtz,
              --
              Rik Wasmus


              Comment

              Working...