0

What i want to do:

Generate single random number which will be passed to functions inside the class.

In other words while calling $this->random both one() and two() should return same value

What is actually happening:

I have multiple random numbers because rand() is getting called multiple times

Important notes:

I want to avoid storing the number inside the session or SQL

How does my code look:

    class Install
    {
        private $random= '';

        public function __construct()
        {
            $this->random = rand(pow(10, 4-1), pow(10, 4)-1);

        }

         public function one()
         {
           echo $this->random; //example value = 3304
         }

         public function two()
         {
           echo $this-random; //example value = 2504
         }

    }
7
  • Do you have any particular reason for ruling out the two best options, session and SQL? Because you have to store them somewhere. Commented Mar 19, 2020 at 18:47
  • Because of sensitivity issue of the token i do not want to pass it to the user browser and because token is temporary and it can be many i do not want to store it in DB as well. Is not there a way to fix the variable after generation and pass it to the functions ? Commented Mar 19, 2020 at 18:48
  • So don't. Session variables are stored on the server. Commented Mar 19, 2020 at 18:49
  • 1
    Wait, do you just want the value to be static across all instances of the class? The code you've provided doesn't do what you claim, and your description is lacking in detail. Commented Mar 19, 2020 at 18:53
  • @Sammitch So basically what i need is to have same random value in both one() and two() functions. i will take a better look on how the session works as well. Commented Mar 19, 2020 at 19:08

1 Answer 1

1

If you just want a consistent random number across both function calls to the same instance in the same request, then your code will work as-is.

If you just want a consistent random number across both function calls to the same instance in the different requests, then you need to use some form of persistent storage like the Session or a DB.

If you just want a consistent random number across both function calls to the different instances in the same request, then you need a class static variable.

class StaticRandom {
    private static $rand = NULL;

    public static function getRand() {
        if( is_null(self::$rand) ) {
            // this is also where you wouldput the session/DB bits if you
            // want to persist across requests.
            self::$rand = rand(pow(10, 4-1), pow(10, 4)-1);
        }
        return self::$rand;
    }

    public function instanceMethodOne() {
        return self::getRand();
    }

    public function instanceMethodTwo() {
        return self::getRand();
    }
}

$a = new StaticRandom();
$b = new StaticRandom();

var_dump(
    $a->instanceMethodOne(),
    $b->instanceMethodTwo()
);

Output:

int(4188)
int(4188)
Sign up to request clarification or add additional context in comments.

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.