|
Posted by Colin Fine on 10/06/06 16:59
Adam wrote:
> Mateusz Markowski wrote:
>> Adam napisal(a):
>>> class matrix {
>>> public $row1 = &new row();
>>> public $row2 = &new row();
>>> public $row3 = &new row();
>>> public $row4 = &new row();
>>> public $row5 = &new row();
>>> public $row6 = &new row();
>>> public $row7 = &new row();
>>> public $row8 = &new row();
>>> public $row9 = &new row();
>>> }
>>>
>>> The problem is is that I guess I cannot use objects within another
>>> class since this code errors. (Parse error: syntax error, unexpected
>>> T_NEW in yadda/yadda/yadda.php). Could someone take a second to set me
>>> right and kindly point me in the right direction? I have a feeling I
>>> am just not looking at this problem correctly.
>> A default class property must be string, number or array. "new"
>> operator is not allowed.
>> Why are creating so many properties instead an array of them? Try:
>>
>> class Matrix {
>> private $rows = array();
>> public function __construct($howmany)
>> {
>> for ($i=0;$i<$howmany;$i++) {
>> $this->rows[$i] =new Row();
>> }
>> }
>> }
>
> I dont know why I am not using an array. I think I am getting confused
> on objects and I am losing my mind :) What is the reasoning behind why
> default class properties only being a string, number, or array? I think
> knowing that would help me understand better. Thanks for your help
> Mateusz!
>
The manual http://www.php.net/manual/en/language.oop5.basic.php actually
says:
"The default value must be a constant expression, not (for example) a
variable, a class member or a function call."
I don't know the reason, but I observe that this way, these initial
values will be the same in every instance of the class. If you were
allowed variables, function calls or new (which is a disguised function
call) they might have different values for every object of the class.
There's nothing wrong with this, but you can get that effect by setting
the value in the constructor, so it is useful to have variable
initialisation serving a different purpose.
Colin
Navigation:
[Reply to this message]
|