0
   class Session{
        protected $git =  md5(rand(1,6)); 
        public function __construct($config = array())
        {
           //// some code
            $ses_id = $this->git;

        }
        public function _start_session()
        {
           //code again..
        }
      }

Here I can't assign a random value like this to variable called git. How can I do this if it is possible? That random value need to be first time generated value only till the time it converts to Null.

4
  • Can you explain what is the issue bit more? Commented May 15, 2015 at 5:13
  • 1
    Are you getting any error? Commented May 15, 2015 at 5:13
  • You cannot write expression(assign value calling by function) to set value of variable outside function of a class.So you need to do that inside function of this class.Constructor is best choice here. Commented May 15, 2015 at 5:34
  • here the issue is ..for every time constructor is called the value of git will change...but i need the first time generated value only every time..how can i do this. Commented May 15, 2015 at 6:08

4 Answers 4

4

Perform random inside your constructor,

class Session{
        protected $git; 
        public function __construct($config = array())
        {
           //// some code
           $this->git =  md5(rand(1,6));
           $ses_id = $this->git;

        }
        public function _start_session()
        {
           //code again..
        }
      }
Sign up to request clarification or add additional context in comments.

2 Comments

here the issue is ..for every time constructor is called the value of git will change...but i need the first time generated value only every time..how can i do this.
to solve this, dont call session class multiple times, just reuse it. When your calling the constructor, your instantiating the class and creating a new instance of it.
0

Try setting the value of your variable in your constructor.

constructor will run every time you create an instance of your class.

try this code:

      class Session{
        protected $git;
        public function __construct($config = array())
        {
           //// some code
           $this->git = md5(rand(1,6));
        }
        public function _start_session()
        {
           //code again..
        }
      }

:)

1 Comment

here the issue is ..for every time constructor is called the value of git will change...but i need the first time generated value only every time..how can i do this.
0

Declare a variable inside a class,initialize the variables in class inside a constructor, which sets the variables once the object for that class is declared anywhere in the code.

I updated this answer if you want to do not change your session variable on each constructor call then use the below procedure.

class Session{
       protected $git;
        public function __construct($config = array())
        {
           $this->git  =  md5(rand(1,6)); 
           if(!isset($_SESSION['ses_id']))
            {
            $_SESSION['ses_id'] = $this->git;
            }
        }
        public function _start_session()
        {
           //code again..
        }
      }

I hope this helps you.

3 Comments

You declare it in the class and instantiate it in the constructor.
here the issue is ..for every time constructor is called the value of git will change...but i need the first time generated value only every time..how can i do this.
@JohnSk, i have updated my answer please check that, now you will be able to have a constant random variable in the session that was generated on the first call of the constructor.
0

Try this using a global variable to track the random number:

class Session{
    protected $git;
    public function __construct($config = array())
    {
        //// some code
        if (!isset($GLOBALS['random_val'])) {
            $GLOBALS['random_val'] = md5(rand(1,6));
        }

        $this->git = $GLOBALS['random_val'];
        $ses_id = $this->git;
        var_dump("Session ID: ".$ses_id);

    }

    public function _start_session()
    {
        //code again..
    }
}


$ses1 = new Session(); // Outputs string(44) "Session ID: 1679091c5a880faf6fb5e6087eb1b2dc"
$ses2 = new Session(); // Outputs string(44) "Session ID: 1679091c5a880faf6fb5e6087eb1b2dc"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.