When "static" isn't "static"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • comp.lang.php

    When "static" isn't "static"

    [PHP]
    function blah($item) {
    if (!isset($baseDi r)) {
    static $baseDir = '';
    $baseDir = $item;
    print_r("baseDi r = $baseDir\n");
    }

    $dirID = opendir($item);
    while (($fyl = readdir($dirID) ) !== false) {
    if (is_dir("$baseD ir/$fyl")) blah($item);
    // DO OTHER STUFF IF IT IS A FILE
    }
    }
    [/PHP]

    I am using PHP 4.3.2 and the following occurs within blah() :

    baseDir = /home/me/stuff
    baseDir = /home/me/stuff/.
    baseDir = /home/me/stuff/./..
    I was under the impression that a static variable retains its value
    throughout a function (or method) recursive memory stack. However, it
    changes everytime because it apparently loses its set value.

    Needless to mention I'm confused. Was I wrong about static; if so,
    what else do I do to retain this value? I cannot use $_SESSION as this
    is command-line PHP instead of API.

    Thanx
    Phil

  • Wayne

    #2
    Re: When "static&qu ot; isn't "static&qu ot;

    On 5 Dec 2005 11:02:31 -0800, "comp.lang. php"
    <phillip.s.powe ll@gmail.com> wrote:
    [color=blue]
    >I was under the impression that a static variable retains its value
    >throughout a function (or method) recursive memory stack. However, it
    >changes everytime because it apparently loses its set value.[/color]

    Try it this way:

    [PHP]
    function blah($item) {
    static $baseDir = '';
    if (!$baseDir) {
    $baseDir = $item;
    print_r("baseDi r = $baseDir\n");
    }

    $dirID = opendir($item);
    while (($fyl = readdir($dirID) ) !== false) {
    if (is_dir("$baseD ir/$fyl")) blah($item);
    // DO OTHER STUFF IF IT IS A FILE
    }
    }
    [/PHP]

    Comment

    • comp.lang.php

      #3
      Re: When &quot;static&qu ot; isn't &quot;static&qu ot;


      Wayne wrote:[color=blue]
      > On 5 Dec 2005 11:02:31 -0800, "comp.lang. php"
      > <phillip.s.powe ll@gmail.com> wrote:
      >[color=green]
      > >I was under the impression that a static variable retains its value
      > >throughout a function (or method) recursive memory stack. However, it
      > >changes everytime because it apparently loses its set value.[/color]
      >
      > Try it this way:
      >
      > [PHP]
      > function blah($item) {
      > static $baseDir = '';
      > if (!$baseDir) {
      > $baseDir = $item;
      > print_r("baseDi r = $baseDir\n");
      > }[/color]

      Sorry that had a worse effect, I'm afraid:

      baseDir = "/home/me/stuff"
      baseDir = "/."
      baseDir = "/.."
      That tried to attempt to alter the root directory instead of the given
      base directory.

      Phil
      [color=blue]
      >
      > $dirID = opendir($item);
      > while (($fyl = readdir($dirID) ) !== false) {
      > if (is_dir("$baseD ir/$fyl")) blah($item);
      > // DO OTHER STUFF IF IT IS A FILE
      > }
      > }
      > [/PHP][/color]

      Comment

      • Oli Filth

        #4
        Re: When &quot;static&qu ot; isn't &quot;static&qu ot;

        comp.lang.php said the following on 05/12/2005 19:02:[color=blue]
        > [PHP]
        > function blah($item) {
        > if (!isset($baseDi r)) {
        > static $baseDir = '';
        > $baseDir = $item;
        > print_r("baseDi r = $baseDir\n");
        > }
        >
        > $dirID = opendir($item);
        > while (($fyl = readdir($dirID) ) !== false) {
        > if (is_dir("$baseD ir/$fyl")) blah($item);
        > // DO OTHER STUFF IF IT IS A FILE
        > }
        > }
        > [/PHP]
        >
        > I am using PHP 4.3.2 and the following occurs within blah() :
        >
        >
        > baseDir = /home/me/stuff
        > baseDir = /home/me/stuff/.
        > baseDir = /home/me/stuff/./..
        >
        >
        > I was under the impression that a static variable retains its value
        > throughout a function (or method) recursive memory stack. However, it
        > changes everytime because it apparently loses its set value.[/color]

        You are using $baseDir before you have declared it static, so PHP treats
        it as non-static.


        --
        Oli

        Comment

        • Sean

          #5
          Re: When &quot;static&qu ot; isn't &quot;static&qu ot;

          I can see what you are trying to do however it's not going to work
          the way you have it. Sure its static var but its going to change every
          time it you recursivly call blah (due to this line $baseDir = $item; &
          this line if (is_dir("$baseD ir/$fyl")) blah($item); );

          (e.g. everytime it finds a dir its going to change the value of
          baseDir).

          Comment

          • Sjoerd

            #6
            Re: When &quot;static&qu ot; isn't &quot;static&qu ot;

            Why do you need this? What do you want to do? Do you want to
            recursively walk trough directories? In that case, here is an example:

            function traverse($dir) {
            $dh = opendir($dir);
            while ($file = readdir($dh)) {
            if ($file{0} == '.') continue;
            $path = $dir.'/'.$file;
            if (is_file($path) ) {
            echo $file."\n";
            } else {
            traverse($path) ;
            }
            }
            closedir($dh);
            }

            Comment

            • comp.lang.php

              #7
              Re: When &quot;static&qu ot; isn't &quot;static&qu ot;

              Wow, ok, that was a surprise! I didn't think isset() had the ability
              to declare a variable on the fly like that. I was under the impression
              that it only checked for instantiation, which would be implied that if
              a variable is not yet declared, it certainly could never be
              instantiated as you cannot instantiate a nonexistent entity.

              That solved the problem but potentially opened up a big PHP logic flaw
              in the process.

              Thanx!
              Phil

              Oli Filth wrote:[color=blue]
              > comp.lang.php said the following on 05/12/2005 19:02:[color=green]
              > > [PHP]
              > > function blah($item) {
              > > if (!isset($baseDi r)) {
              > > static $baseDir = '';
              > > $baseDir = $item;
              > > print_r("baseDi r = $baseDir\n");
              > > }
              > >
              > > $dirID = opendir($item);
              > > while (($fyl = readdir($dirID) ) !== false) {
              > > if (is_dir("$baseD ir/$fyl")) blah($item);
              > > // DO OTHER STUFF IF IT IS A FILE
              > > }
              > > }
              > > [/PHP]
              > >
              > > I am using PHP 4.3.2 and the following occurs within blah() :
              > >
              > >
              > > baseDir = /home/me/stuff
              > > baseDir = /home/me/stuff/.
              > > baseDir = /home/me/stuff/./..
              > >
              > >
              > > I was under the impression that a static variable retains its value
              > > throughout a function (or method) recursive memory stack. However, it
              > > changes everytime because it apparently loses its set value.[/color]
              >
              > You are using $baseDir before you have declared it static, so PHP treats
              > it as non-static.
              >
              >
              > --
              > Oli[/color]

              Comment

              • comp.lang.php

                #8
                Re: When &quot;static&qu ot; isn't &quot;static&qu ot;

                No I'm sorry I think you misunderstood my problem. I wound up having
                it solved earlier; the problem had to do with the way I was using the
                static option, which I still believe illuminates a potential PHP logic
                flaw when it comes to using "static".

                Phil

                Sjoerd wrote:[color=blue]
                > Why do you need this? What do you want to do? Do you want to
                > recursively walk trough directories? In that case, here is an example:
                >
                > function traverse($dir) {
                > $dh = opendir($dir);
                > while ($file = readdir($dh)) {
                > if ($file{0} == '.') continue;
                > $path = $dir.'/'.$file;
                > if (is_file($path) ) {
                > echo $file."\n";
                > } else {
                > traverse($path) ;
                > }
                > }
                > closedir($dh);
                > }[/color]

                Comment

                • Oli Filth

                  #9
                  Re: When &quot;static&qu ot; isn't &quot;static&qu ot;

                  comp.lang.php said the following on 05/12/2005 19:38:[color=blue]
                  > Wow, ok, that was a surprise! I didn't think isset() had the ability
                  > to declare a variable on the fly like that.[/color]

                  It doesn't.
                  [color=blue]
                  > I was under the impression
                  > that it only checked for instantiation[/color]

                  It does.
                  [color=blue]
                  > which would be implied that if
                  > a variable is not yet declared, it certainly could never be
                  > instantiated as you cannot instantiate a nonexistent entity.[/color]

                  ??
                  [color=blue]
                  >
                  > That solved the problem but potentially opened up a big PHP logic flaw
                  > in the process.
                  >
                  >
                  > Oli Filth wrote:
                  >[color=green]
                  >>comp.lang.p hp said the following on 05/12/2005 19:02:
                  >>[color=darkred]
                  >>>[PHP]
                  >>>function blah($item) {
                  >>> if (!isset($baseDi r)) {
                  >>> static $baseDir = '';
                  >>> $baseDir = $item;
                  >>> print_r("baseDi r = $baseDir\n");
                  >>> }
                  >>>
                  >>> $dirID = opendir($item);
                  >>> while (($fyl = readdir($dirID) ) !== false) {
                  >>> if (is_dir("$baseD ir/$fyl")) blah($item);
                  >>> // DO OTHER STUFF IF IT IS A FILE
                  >>> }
                  >>>}
                  >>>[/PHP]
                  >>>
                  >>>I am using PHP 4.3.2 and the following occurs within blah() :
                  >>>
                  >>>
                  >>>baseDir = /home/me/stuff
                  >>>baseDir = /home/me/stuff/.
                  >>>baseDir = /home/me/stuff/./..
                  >>>
                  >>>
                  >>>I was under the impression that a static variable retains its value
                  >>>throughout a function (or method) recursive memory stack. However, it
                  >>>changes everytime because it apparently loses its set value.[/color]
                  >>
                  >>You are using $baseDir before you have declared it static, so PHP treats
                  >>it as non-static.
                  >>
                  >>
                  >>--
                  >>Oli[/color]
                  >
                  >[/color]


                  --
                  Oli

                  Comment

                  • comp.lang.php

                    #10
                    Re: When &quot;static&qu ot; isn't &quot;static&qu ot;

                    I'm sorry I do not understand what you are trying to say.

                    Phil

                    Oli Filth wrote:[color=blue]
                    > comp.lang.php said the following on 05/12/2005 19:38:[color=green]
                    > > Wow, ok, that was a surprise! I didn't think isset() had the ability
                    > > to declare a variable on the fly like that.[/color]
                    >
                    > It doesn't.
                    >[color=green]
                    > > I was under the impression
                    > > that it only checked for instantiation[/color]
                    >
                    > It does.
                    >[color=green]
                    > > which would be implied that if
                    > > a variable is not yet declared, it certainly could never be
                    > > instantiated as you cannot instantiate a nonexistent entity.[/color]
                    >
                    > ??
                    >[color=green]
                    > >
                    > > That solved the problem but potentially opened up a big PHP logic flaw
                    > > in the process.
                    > >
                    > >
                    > > Oli Filth wrote:
                    > >[color=darkred]
                    > >>comp.lang.p hp said the following on 05/12/2005 19:02:
                    > >>
                    > >>>[PHP]
                    > >>>function blah($item) {
                    > >>> if (!isset($baseDi r)) {
                    > >>> static $baseDir = '';
                    > >>> $baseDir = $item;
                    > >>> print_r("baseDi r = $baseDir\n");
                    > >>> }
                    > >>>
                    > >>> $dirID = opendir($item);
                    > >>> while (($fyl = readdir($dirID) ) !== false) {
                    > >>> if (is_dir("$baseD ir/$fyl")) blah($item);
                    > >>> // DO OTHER STUFF IF IT IS A FILE
                    > >>> }
                    > >>>}
                    > >>>[/PHP]
                    > >>>
                    > >>>I am using PHP 4.3.2 and the following occurs within blah() :
                    > >>>
                    > >>>
                    > >>>baseDir = /home/me/stuff
                    > >>>baseDir = /home/me/stuff/.
                    > >>>baseDir = /home/me/stuff/./..
                    > >>>
                    > >>>
                    > >>>I was under the impression that a static variable retains its value
                    > >>>throughout a function (or method) recursive memory stack. However, it
                    > >>>changes everytime because it apparently loses its set value.
                    > >>
                    > >>You are using $baseDir before you have declared it static, so PHP treats
                    > >>it as non-static.
                    > >>
                    > >>
                    > >>--
                    > >>Oli[/color]
                    > >
                    > >[/color]
                    >
                    >
                    > --
                    > Oli[/color]

                    Comment

                    • Oli Filth

                      #11
                      Re: When &quot;static&qu ot; isn't &quot;static&qu ot;

                      comp.lang.php said the following on 05/12/2005 20:19:[color=blue]
                      > I'm sorry I do not understand what you are trying to say.
                      >
                      >
                      > Oli Filth wrote:
                      >[color=green]
                      >>comp.lang.p hp said the following on 05/12/2005 19:38:
                      >>[color=darkred]
                      >>>Wow, ok, that was a surprise! I didn't think isset() had the ability
                      >>>to declare a variable on the fly like that.[/color]
                      >>
                      >>It doesn't.[/color][/color]

                      As in: isset() doesn't have the ability to declare a variable on the fly.
                      [color=blue][color=green]
                      >>
                      >>[color=darkred]
                      >>> I was under the impression
                      >>>that it only checked for instantiation[/color]
                      >>
                      >>It does.[/color][/color]

                      As in: isset() does only check for instantiation.

                      [color=blue][color=green]
                      >>[color=darkred]
                      >>>which would be implied that if
                      >>>a variable is not yet declared, it certainly could never be
                      >>>instantiat ed as you cannot instantiate a nonexistent entity.[/color]
                      >>
                      >>??[/color][/color]

                      As in: this sentence makes no sense!!



                      --
                      Oli

                      Comment

                      • Chung Leong

                        #12
                        Re: When &quot;static&qu ot; isn't &quot;static&qu ot;

                        What Oli meant was that the line "static $baseDir" doesn't take effect
                        until it's reached, hence !isset($baseDir ) will always be true in your
                        code. Better think of it as an operator than a declaration.

                        Comment

                        Working...