[SOLVED] Weird static variable behavior
Posted: Tue Jul 03, 2007 8:36 am
Hy,
I'm developing an application where I'm getting a weird behavior from the static variable:
-----------------------------------------------------------
-----------------------------------------------------------
I call this function using something like $var =& class::getConfig();
While calling it the first time, the values are stored correctly within the $instance ( since its built by another method within the class ).
After that, I do another $var =& class::getConfig() where I intend to return $instance has a reference so I can update the values within it and have it avaiable at other functions/methods/whatever...
But here the weird behavior starts... while I'm in the method/function where I update the $var ( reference of $instance ) I can get the updated values stored at $instance, as soon as I leave to another method/function, the $instance looses the changes and only stores the values created
at the first call ( a simples example of a var_dump() ):
1. First call ( method/function 1 ): stored values
2. Second call ( method/function 2 where I update $instance ): stored values and updated ones
3. Third call ( method/function 3 ): only stored values again and the new ones ( inserted at method/function 2 ) are lost...
I've searched around and no answer was found about this strange behavior ( even tought I'm returning refernce it behaves like returning only values )... so I did some "bad coding" and I came up with this function:
-----------------------------------------------------------
-----------------------------------------------------------
As stupid as this code may seem, it works, and now the "var_dump()" would result in something like:
1. First call ( method/function 1 ): stored values
2. Second call ( method/function 2 where I update $instance ): stored values and updated ones
3. Third call ( method/function 3 ): stored values and the new values ( inserted at the function/method 2 ) are still there, and no data was lost.
Can someone explain me whats the reason for this strange behavior ?
Best regards,
Miguel Simões
I'm developing an application where I'm getting a weird behavior from the static variable:
-----------------------------------------------------------
Code: Select all
public function &getConfig( $file = NULL , $type = 'default' ) {
static $instance;
if( is_null( $file ) ) $file = myBasePath . DS . 'myConfiguration.php';
if( !is_object( $instance ) ) {
$instance =& self::_createConfig( $file , $type );
}
return $instance;
}I call this function using something like $var =& class::getConfig();
While calling it the first time, the values are stored correctly within the $instance ( since its built by another method within the class ).
After that, I do another $var =& class::getConfig() where I intend to return $instance has a reference so I can update the values within it and have it avaiable at other functions/methods/whatever...
But here the weird behavior starts... while I'm in the method/function where I update the $var ( reference of $instance ) I can get the updated values stored at $instance, as soon as I leave to another method/function, the $instance looses the changes and only stores the values created
at the first call ( a simples example of a var_dump() ):
1. First call ( method/function 1 ): stored values
2. Second call ( method/function 2 where I update $instance ): stored values and updated ones
3. Third call ( method/function 3 ): only stored values again and the new ones ( inserted at method/function 2 ) are lost...
I've searched around and no answer was found about this strange behavior ( even tought I'm returning refernce it behaves like returning only values )... so I did some "bad coding" and I came up with this function:
-----------------------------------------------------------
Code: Select all
public function &getConfig( $file = NULL , $type = 'default' ) {
static $instance;
if( is_null( $file ) ) $file = myBasePath . DS . 'myConfiguration.php';
if( !is_object( $instance ) ) {
$tmp =& self::_createConfig( $file , $type );
unset( $instance );
static $instance;
$instance = $tmp;
}
return $instance;
}As stupid as this code may seem, it works, and now the "var_dump()" would result in something like:
1. First call ( method/function 1 ): stored values
2. Second call ( method/function 2 where I update $instance ): stored values and updated ones
3. Third call ( method/function 3 ): stored values and the new values ( inserted at the function/method 2 ) are still there, and no data was lost.
Can someone explain me whats the reason for this strange behavior ?
Best regards,
Miguel Simões