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?
Answer
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);
}
No comments:
Post a Comment