0

When I use count while defining a private variable in a php class, It throws an error. Here is my class

class setup {

private $acctListArr = array(4533,4534,4535,4536,4537,4538,4539,4540,4541,4542,4543,4544,4545,4546,4547,4548,4549,4550,4551,4552,4553,4554,4555,4556,4557,4559,4560,4561,4562,4563,4564,4565,4566,4567,4568,4569,4570,4571,4572,4573,4574,4575,45766,4577,4578,4579,4580,4581,4582,4583,4584,4585,4586,4587,4588,4589,4590,4591,4592,4593,4594,4595,4596,4597,4598,4599,4600,4601,4602,4603,4604,4605,4606,4607,4608,4609,4610,4611,4612,4613,4614,4615,4616,4617,4618);
private $acctsInList = count($this->acctListArr);

public function __construct() {
}

}

When visiting the class in a browser it throw an error:

Parse error: syntax error, unexpected '(', expecting ',' or ';' in C:\xampp\htdocs\dev.virtualnerd.com_classes\setup.class.php on line 7

Line 7 being private $acctsInList = count($this->acctListArr);

Can you not define a private variable this way?

hakre
  • 193,403
  • 52
  • 435
  • 836
bretterer
  • 5,693
  • 5
  • 32
  • 53
  • 1
    Just add `$this->acctsInList = count($this->acctListArr);` to the constructor. – popthestack Jan 06 '12 at 20:16
  • 2
    possible duplicate of [Attribute declarations in a class definition can only be constant values, not expressions.](http://stackoverflow.com/questions/2671928/workaround-for-basic-syntax-not-being-parsed) – mario Jan 06 '12 at 20:17

4 Answers4

11

No, you can't. Class properties cannot be defined with computed values (e.g., function calls).

From the docs:

This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

As also pointed out by others, you could instead do that calculation and assign it in the constructor.

Wiseguy
  • 20,522
  • 8
  • 65
  • 81
5

You can't call a function in the declaration of an instance variable. Instead assign this in the constructor like so:

 private $acctListArr = array(4533,4534,4535,4536,4537,4538,4539,4540,4541,4542,4543,4544,4545,4546,4547,4548,4549,4550,4551,4552,4553,4554,4555,4556,4557,4559,4560,4561,4562,4563,4564,4565,4566,4567,4568,4569,4570,4571,4572,4573,4574,4575,45766,4577,4578,4579,4580,4581,4582,4583,4584,4585,4586,4587,4588,4589,4590,4591,4592,4593,4594,4595,4596,4597,4598,4599,4600,4601,4602,4603,4604,4605,4606,4607,4608,4609,4610,4611,4612,4613,4614,4615,4616,4617,4618);

 private $acctsInList;

 public function __construct() {
     $this->acctsInList = count($this->acctListArr);
 }
Wes Crow
  • 2,971
  • 20
  • 24
0

What you want is

class setup {

private $acctListArr = array(4533,4534,4535,4536,4537,4538,4539,4540,4541,4542,4543,4544,4545,4546,4547,4548,4549,4550,4551,4552,4553,4554,4555,4556,4557,4559,4560,4561,4562,4563,4564,4565,4566,4567,4568,4569,4570,4571,4572,4573,4574,4575,45766,4577,4578,4579,4580,4581,4582,4583,4584,4585,4586,4587,4588,4589,4590,4591,4592,4593,4594,4595,4596,4597,4598,4599,4600,4601,4602,4603,4604,4605,4606,4607,4608,4609,4610,4611,4612,4613,4614,4615,4616,4617,4618);

private $acctsInList = 0;

public function __construct() {
  $this->acctsInList = count($this->acctListArr);
}


}
Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
0

You Can write the class like this

class setup {

    private $acctListArr = array(4533,4534,4535,4536,4537,4538,4539,4540,4541,4542,4543,4544,4545,4546,4547,4548,4549,4550,4551,4552,4553,4554,4555,4556,4557,4559,4560,4561,4562,4563,4564,4565,4566,4567,4568,4569,4570,4571,4572,4573,4574,4575,45766,4577,4578,4579,4580,4581,4582,4583,4584,4585,4586,4587,4588,4589,4590,4591,4592,4593,4594,4595,4596,4597,4598,4599,4600,4601,4602,4603,4604,4605,4606,4607,4608,4609,4610,4611,4612,4613,4614,4615,4616,4617,4618);

    private $acctsInList = 0;

    public function __construct() {
        $this->acctsInList = count($this->acctListArr);
    }
}
Dan
  • 10,531
  • 2
  • 36
  • 55
Aboodred1
  • 1,353
  • 1
  • 10
  • 22