Global variable not updating inside static Class method in PHP -


i have problem global variables in php. problem global variable change inside static class method isn't updating outside method.

i've included code:

test.php

define( 'app_id', 'testing' ); $_app = array( 'test' => 'test value' ); include ('appsettings.class.php'); appsettings::initapplication(); 

appsettings.class.php

class appsettings {   public static function initapplication()   {     global $_app;     session_start();      // code here initializtions     self::initappengine(); echo '<pre>inside initapplication: '; print_r($_app); echo '<pre>directly printing session variable: '; print_r($_session[app_id] );   }    private static function initappengine()   {     global $_app;      if( isset($_session[app_id]) )     {       $_app = &$_session[app_id];     }     else     {       $_session[app_id] = array( 'abcd' => 'hello', 'app_id' => app_id );       $_app = &$_session[app_id]; die("refresh page");     }      if ( !isset( $_app['uid'] ) )       $_app['uid'] = 0;  echo '<pre>inside initappengine: '; print_r($_app);   } } 

the old value of $_app coming instead of new 1 inside initapplication. can point out i'm doing wrong?

thanks in advance,

this quite interesting. first of all, note seems have nothing static methods:

$_session['test'] = array("test value superglobal"); $_app = array('test' => "test value directly assigned");  class appsettings {   public static function initapplication()   {     global $_app;     $_app = &$_session['test'];     echo '<pre>inside initapplication: '; print_r($_app);   }    public function initapplicationnonstatic()   {     global $_app;     $_app = &$_session['test'];     echo '<pre>inside initapplicationnonstatic: '; print_r($_app);   } }  echo '<pre>before calling initapplication: '; print_r($_app); appsettings::initapplication(); echo '<pre>after calling initapplication: '; print_r($_app); echo '<pre>before calling initapplicationnonstatic: '; print_r($_app); $appsettings = new appsettings(); $appsettings->initapplicationnonstatic(); echo '<pre>after calling initapplicationnonstatic: '; print_r($_app); 

result:

before calling initapplication: array (     [test] => test value directly assigned ) inside initapplication: array (     [0] => test value superglobal ) after calling initapplication: array (     [test] => test value directly assigned ) before calling initapplicationnonstatic: array (     [test] => test value directly assigned ) inside initapplicationnonstatic: array (     [0] => test value superglobal ) after calling initapplicationnonstatic: array (     [test] => test value directly assigned ) 

but works:

$_session['test'] = array("test value superglobal"); $_app = array('test' => "test value directly assigned");  class appsettings2 {   public function initapplicationnonstatic()   {     $globals['_app'] = &$_session['test']; // reference     echo '<pre>inside initapplicationnonstatic: '; print_r($globals['_app']);   } }  echo '<pre>before calling initapplicationnonstatic: '; print_r($_app); $appsettings2 = new appsettings2(); $appsettings2->initapplicationnonstatic(); echo '<pre>after calling initapplicationnonstatic: '; print_r($_app); $_session['test'] = array("test value superglobal altered"); echo '<pre>after altering superglobal: '; print_r($_app); 

result:

before calling initapplicationnonstatic: array (     [test] => test value directly assigned ) inside initapplicationnonstatic: array (     [0] => test value superglobal ) after calling initapplicationnonstatic: array (     [0] => test value superglobal ) after altering superglobal: array (     [0] => test value superglobal altered ) 

and works, too:

$_session['test'] = array("test value superglobal"); $_app = array('test' => "test value directly assigned");  class appsettings2 {   public function initapplicationnonstatic()   {     global $_app;     $_app = $_session['test']; // value     echo '<pre>inside initapplicationnonstatic: '; print_r($_app);   } }  echo '<pre>before calling initapplicationnonstatic: '; print_r($_app); $appsettings2 = new appsettings2(); $appsettings2->initapplicationnonstatic(); echo '<pre>after calling initapplicationnonstatic: '; print_r($_app); $_session['test'] = array("test value superglobal altered"); echo '<pre>after altering superglobal: '; print_r($_app); 

result:

before calling initapplicationnonstatic: array (     [test] => test value directly assigned ) inside initapplicationnonstatic: array (     [0] => test value superglobal ) after calling initapplicationnonstatic: array (     [0] => test value superglobal ) after altering superglobal: array (     [0] => test value superglobal // expected, since assigned value ) 

so, seems whenever want assign reference global variable inside function or method, have use $globals['_app'] syntax , cannot use global $_app. if don't need assignment reference, $globals['_app'] , global $_app behave same.

i'm not sure why so; some pages refer equivalence of these 2 constructs:

global $example; $example =& $globals['example']; 

this might lead right track; however, hope can troubleshoot problem answer.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -