0

I had one php class in namespace App\Http\Lib as GISApplication which contains static data members. I want to set & access values of that static data members from different classes outside of namespace. How Can I set & access that values.

<?php
namespace App\Http\Lib; 

class GISApplication
{
      public static $LOGINUSER_TYPE;
      public static $USERMODULE;

      // More Static Data Members

}

Suppose I want to set values of these members from LoginController class in App\Http\Controllers namespace & access that values from different classes in App\Http\Operations namespace.

I set values from LoginController class in App\Http\Controllers namespace AS

GISApplication::$LOGINUSER_TYPE = $auth->type;
GISApplication::$USERMODULE = $auth->module;

When I access values from FrontOperation class in App\Http\Operations AS

GISApplication::$LOGINUSER_TYPE

I will getting blank value.

Please suggest.

Ganesh UP
  • 107
  • 6
  • 18
  • This should be fine from syntactical point of view. Check whether $auth->type and $auth->module really returns non-blank values in point of assignment and also check whether you are accessing static variable after assignment. – hradecek Jan 27 '17 at 11:59
  • @IvoHrádek : I already checked values within $auth->type & $auth->module, both contains non-blank string values. Also I am sure that I am accessing variables after assignment. Please suggest any change that i can't figure out. – Ganesh UP Jan 30 '17 at 05:11
  • I am tried with getter & setter methods also, but no luck. public static function setLoginUserType($UserType) { self::$LOGINUSER_TYPE = $UserType; } public static function getLoginUserType() { return self::$LOGINUSER_TYPE; // "QWERTY"; } Any suggestion. Sorry for not able to format code in comment well. – Ganesh UP Jan 30 '17 at 06:41

1 Answers1

0

Static variables in PHP does not retain value after page refresh/postback. So every time I try to access value, I am getting blank value. I got this idea from below link

How do I change a static variables value in PHP?

To solve the problem I used Session variable in LoginController as

$_SESSION['LOGINUSERTYPE']  = $auth->type;
$_SESSION['USERMODULE'] = $auth->module;

To access in different controllers, simply use

$user= $_SESSION['LOGINUSERTYPE'];

If any other suitable way, please suggest.

Community
  • 1
  • 1
Ganesh UP
  • 107
  • 6
  • 18