Fede E.
Fede E.

Reputation: 1918

Why am I getting a syntax error in setting this Array into a class?

I am getting a syntax error but I don;t understand exactly why, here is my code:

class VerifyEmail {

     private $ip_pool = array("167.114.48.81","167.114.48.82"....);
     private $ip_to_use = $this->ip_pool[array_rand($this->ip_pool)]; //ERROR HERE
     .....

I tried also:

     private $ip_to_use = $ip_pool[array_rand($ip_pool)];

with no luck.

Am I missing something? Or you cannot do an array rand of a private variable when setting up the variables?

Thanks!

Upvotes: 1

Views: 53

Answers (2)

Scuzzy
Scuzzy

Reputation: 12332

I get the following notice in my IDE for that line

expression is not allowed as a field default value

I can only suggest moving your rand call into the __construct() method

class VerifyEmail {

  private $ip_pool = array( "167.114.48.81", "167.114.48.82");
  private $ip_to_use;

  public function __construct() {
    $this->ip_to_use = $this->ip_pool[ array_rand( $this->ip_pool ) ];
  }

}

var_dump( new VerifyEmail() );

Upvotes: 1

Obsidian Age
Obsidian Age

Reputation: 42384

You're currently trying to access the array as an index of the array itself.

Considering you're just trying to assign one string from within the $ip_pool array to $ip_to_use, all you need is $ip_to_use = array_rand($this->ip_pool).

class VerifyEmail {  
  private $ip_pool = array("167.114.48.81","167.114.48.82"....);
  private $ip_to_use = array_rand($this->ip_pool);
  ...  

Hope this helps! :)

Upvotes: 1

Related Questions