Re: Array inside Class
| From: | Greg Beaver | Date: | Fri, 10 Sep 2004 20:27:32 +0000 |
| Subject: | Re: Array inside Class | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-196352@lists.php.net to get a copy of this message | ||
M. Sokolewicz wrote:
This is not entirely accurate. <?php class foo {To summarize: <?php class Liste { var $input2; var $input;The reason being, for this, that inside a class property definition, only static values are allowed. This has to do with the Static use of a class. Imagine the following (PHP 4): --- PHP 4 --- class foo { var $a = 2; var $b = 5; } --- end --- or --- PHP 5 --- class foo { public $a = 2; public $b = 5; } --- end --- Now, when someone uses it statically, he/she can call them like: --- echo foo::a;function Liste() { /* or, if you're using php5: public function__construct() { */$this->input = array(1,2,3); $this->input2 = array_pad($this->input,10,1); }}
public $a = 2;
public $b = 5;
}
echo foo::a;
results in:
Fatal error: Undefined class constant 'a' in PHPDocument1 on line 7
<?php
class foo {
public $a = 2;
public $b = 5;
}
echo foo::$a;
results in:
Fatal Error: Access to undeclared static property: foo::$a in PHPDocument1 on line 7
<?php
class foo {
static $a = 2;
public $b = 5;
}
echo foo::$a;
results in:
2
The reasoning behind not allowing complex expressions in variable declarations is that they must be evaluated at compile time, *before* any functions, classes, or other complex structures have been parsed and therefore created, otherwise php would be either incredibly unstable or much slower.
Greg