Error calling function Rand() in setting a property

Asked

Viewed 108 times

-1

I have a "Forgot Password" code. And at the time of generating the random number to send to email, is giving me this error:

Parse error: syntax error, Unexpected '(', expecting ',' or ';' on line 9

Imagem do erro

someone can help me with that?

//Criar um código aleátorio
var $min    = 000001;
var $max    = 100000;
var $codigo = rand($min,$max);
//fim criar código aleátorio
  • 1

    Post the error line and previous.

  • gave an up in the post!

  • 1

    Well ... I suspect this is class definition, in php you would not assign a value that way would have to do this in the constructor. Obs: var was used in php4 ie if the code is not legacy do not use this, exchange for one of the access modifiers.

  • @rray yes has a class just for the reason that I need to access the var $code again! Outside that would not need!

  • 1

    Explaining what @rray said, you can’t call functions (like rand) in the definition of class properties, only in the constructor.

  • @bfavaretto ah yes now it was clear, but how could I access this rand in another builder ?

  • 1

    I think that that answer solves or helps.

Show 2 more comments

2 answers

2


You can’t call functions (like rand) in the definition of class properties, only in the constructor. The constructor is a special function executed whenever an instance of the class is created, and in PHP has the name __construct. For example:

class MinhaClasse {

    private $min = 1;
    private $max = 10;
    private $valor;

    function __construct() {
        $this->valor = rand($this->min, $this->max);
    }
}
  • This solves but this error appeared to me! Fatal error: Call to undefined function enviacodigo() | echo enviacodigo($email);

  • 2

    There seems another problem, perhaps it is the case to post another question. In echo enviacodigo($email), you cannot call the function that, because it is a class method, not a global function. From within the class, you should call it with $this->enviacodigo($email).

-1

In PHP the function of generating random numbers is as follows:

mt_rand(inicio, fim)

So, test with your code like this:

//Criar um código aleátorio
var $min    = 000001;
var $max    = 100000;
var $codigo = mt_rand($min,$max);
//fim criar código aleátorio

The function mt_rand() generates an integer using the "Mersenne Twister" algorithm. Also, it is 4 times faster than the rand() that you were trying to use.

Source: W3 Schools website.

  • You cannot assign a class property with certain expressions or another variable.

  • @Dante continues the error Dante. @@rray I could not understand the answer you sent me

  • But it could directly inform the values in mt_rand() and use elsewhere. (Call straight into the constructor and pass instead of putting in his code)

  • Lucas, this Random number you can generate every time you run the constructor of your page and use this stored value. Or, pass this responsibility on to your Controller(Which would be much better)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.