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):On Fri, 10 Sep 2004 14:26:22 -0400, John Holmes <[EMAIL PROTECTED]> wrote:
From: "dirk" <[EMAIL PROTECTED]>
can anyone explain to me, why I can't resize an array inside a class? Sample Code:
<?php class Liste { var $input = array (1,2,3); var $input2 = array_pad ($input,10, 1); } ?>
Output: Parse error: parse error, unexpected '(', expecting ',' or ';' in /srv/www/htdocs/stundenplan/stpoo.php on line 4
You can't assign values like that.
Try this: var $input = array(1,2,3); var $input2 = array(); $this->input2 = array_pad($this->input,10,1);
To summarize:
<?php class Liste {
var $input2; var $input;
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); } }
--- 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; --- returning 2.
Now, if someone were allowed to use non-static values, eg. those supplied by functions, one could do:
--- PHP 4 ---
class bar {
var $a = rand();
}
--- end ---
or
--- PHP 5 ---
class bar {
public $a = rand();
}
--- end ---
Now, when someone would be calling the class properties STATICALLY, what would one get? Certainly not static results. Correct?
echo bar::a;
might return 2, or 26565984 or anything else. It also might return different values with each call to it.
The behaviour is not static. It is dynamic. This would thus not comply with the idea "Static" calling. Which is also the reason not to allow it.
The workaround given by Wouter is a common way of getting around this. That change however only makes the variables work when not the class is called, but the object instantiated from it. In other words, when it is called dynamically. When it's called dynamically, the object is stored seperatly from the class, and represents a sibling of the class, and thus MAY contain different property values than the other siblings of that class.
Hope that makes it a bit clearer. - Tul
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php