PHP Ojbect Reference in a functions static variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ryan Hubbard

    PHP Ojbect Reference in a functions static variable

    Could someone provide me with some code to hold an object reference in
    a static variable in a function.

    function a(&$t){
    static $b;

    if(is_object($b )){
    print "IN - $b->logfile";
    } else {
    print "Setting<BR >";
    $b = &$t;
    print "IN - $b->logfile<BR>" ;
    }
    }

    calling a with a object reference then should hold its reference and
    if called with no args it should print out the references variable.
    But when called with no aguments the static varible b is null. Can
    some one help be out here. Thanks

    $text->logfile = "TEST";
    a(&$test);
    a();

    output
    ------
    Setting
    IN - TEST
    Setting
    IN - TEST
  • Amir Khawaja

    #2
    Re: PHP Ojbect Reference in a functions static variable

    Ryan Hubbard wrote:[color=blue]
    > Could someone provide me with some code to hold an object reference in
    > a static variable in a function.
    >
    > function a(&$t){
    > static $b;
    >
    > if(is_object($b )){
    > print "IN - $b->logfile";
    > } else {
    > print "Setting<BR >";
    > $b = &$t;
    > print "IN - $b->logfile<BR>" ;
    > }
    > }
    >
    > calling a with a object reference then should hold its reference and
    > if called with no args it should print out the references variable.
    > But when called with no aguments the static varible b is null. Can
    > some one help be out here. Thanks
    >
    > $text->logfile = "TEST";
    > a(&$test);
    > a();
    >
    > output
    > ------
    > Setting
    > IN - TEST
    > Setting
    > IN - TEST[/color]

    Calling a() without any parameters causes PHP to issue a warning because
    you have defined a required parameter for the function "a". When you
    declare a variable in a function to be static, it is only static for the
    life of the function. Once the function exits, all values associated
    with it are marked for deletion. Try the following modification to your
    code and you should see the difference.

    <?php

    static $b;

    function a(&$t){
    if(is_object($G LOBALS['b'])){
    print "IN - " . $GLOBALS['b']->logfile . "\n";
    } else {
    print "Setting\n" ;
    $GLOBALS['b'] =& $t;
    print "IN - " . $GLOBALS['b']->logfile . "\n";
    }
    }

    class test {
    var $logfile;
    }


    $test = new test();
    $test->logfile = "TEST";
    a($test);
    a($test);

    ?>

    output
    -----------
    Setting
    IN - TEST
    IN - TEST


    --
    Amir Khawaja.

    ----------------------------------
    Rules are written for those who lack the ability to truly reason, But
    for those who can, the rules become nothing more than guidelines, And
    live their lives governed not by rules but by reason.
    - James McGuigan

    Comment

    • Ryan Hubbard

      #3
      Re: PHP Ojbect Reference in a functions static variable

      Static variables are not deleted upon exiting the function. The value
      will stay and only the initial static declaration inside the function
      are run the first time through the function. Example

      function test(){
      static a = 0;
      a++;
      return a;
      }

      print test();
      print test();
      print test();

      output
      ------
      1
      2
      3

      Yes it will work with a global variable but I wish not to use globals
      for a very good reason. The problem here are static variables
      combined with object references. Can anyone help.

      Thanks


      Amir Khawaja <amir@gorebels. net> wrote in message news:<EKPUb.252 99$QJ3.875@fed1 read04>...[color=blue]
      > Ryan Hubbard wrote:[color=green]
      > > Could someone provide me with some code to hold an object reference in
      > > a static variable in a function.
      > >
      > > function a(&$t){
      > > static $b;
      > >
      > > if(is_object($b )){
      > > print "IN - $b->logfile";
      > > } else {
      > > print "Setting<BR >";
      > > $b = &$t;
      > > print "IN - $b->logfile<BR>" ;
      > > }
      > > }
      > >
      > > calling a with a object reference then should hold its reference and
      > > if called with no args it should print out the references variable.
      > > But when called with no aguments the static varible b is null. Can
      > > some one help be out here. Thanks
      > >
      > > $text->logfile = "TEST";
      > > a(&$test);
      > > a();
      > >
      > > output
      > > ------
      > > Setting
      > > IN - TEST
      > > Setting
      > > IN - TEST[/color]
      >
      > Calling a() without any parameters causes PHP to issue a warning because
      > you have defined a required parameter for the function "a". When you
      > declare a variable in a function to be static, it is only static for the
      > life of the function. Once the function exits, all values associated
      > with it are marked for deletion. Try the following modification to your
      > code and you should see the difference.
      >
      > <?php
      >
      > static $b;
      >
      > function a(&$t){
      > if(is_object($G LOBALS['b'])){
      > print "IN - " . $GLOBALS['b']->logfile . "\n";
      > } else {
      > print "Setting\n" ;
      > $GLOBALS['b'] =& $t;
      > print "IN - " . $GLOBALS['b']->logfile . "\n";
      > }
      > }
      >
      > class test {
      > var $logfile;
      > }
      >
      >
      > $test = new test();
      > $test->logfile = "TEST";
      > a($test);
      > a($test);
      >
      > ?>
      >
      > output
      > -----------
      > Setting
      > IN - TEST
      > IN - TEST[/color]

      Comment

      Working...