sytax error, but where?

sytax error, but where?

am 29.10.2005 23:54:20 von Carlton

41 class C_Category{
42 // var $name = array(); changed to var $catname="";
43 var $catname = "";
44 $PictureList[] = new C_PictureList();
45 }
the above code (line numbers added) gives the error message:-
Parse error: parse error, unexpected T_VARIABLE, expecting
T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /var/www/html/lc.php on
line 44
The class C_PictureList is defined prior to line 41

Is this the correct syntax for an array of objects?
T.I.A.

Re: sytax error, but where?

am 29.10.2005 23:59:03 von catch

carlton said the following on 29/10/2005 22:54:
> 41 class C_Category{
> 42 // var $name = array(); changed to var $catname="";
> 43 var $catname = "";
> 44 $PictureList[] = new C_PictureList();
> 45 }
> the above code (line numbers added) gives the error message:-
> Parse error: parse error, unexpected T_VARIABLE, expecting
> T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /var/www/html/lc.php on
> line 44
> The class C_PictureList is defined prior to line 41
>

You need "var $PictureList[]...", or if PHP 5, then public, protected or
private.

--
Oli

Re: sytax error, but where?

am 30.10.2005 11:28:43 von Carlton

Oli Filth wrote:
> carlton said the following on 29/10/2005 22:54:
>
>> 41 class C_Category{
>> 42 // var $name = array(); changed to var $catname="";
>> 43 var $catname = ""; 44 $PictureList[] = new C_PictureList();
>> 45 }
>> the above code (line numbers added) gives the error message:-
>> Parse error: parse error, unexpected T_VARIABLE, expecting
>> T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /var/www/html/lc.php on
>> line 44
>> The class C_PictureList is defined prior to line 41
>>
>
> You need "var $PictureList[]...", or if PHP 5, then public, protected or
> private.
>
I added the var to line 44, and now get the error
Parse error: parse error, unexpected '[', expecting ',' or ';' in
/var/www/html/lc.php on line 44

line 44 is var $PictureList[] = new C_PictureList();

Re: sytax error, but where?

am 30.10.2005 11:32:33 von Carlton

Janwillem Borleffs wrote:
> carlton wrote:
>
>>I added the var to line 44, and now get the error
>>Parse error: parse error, unexpected '[', expecting ',' or ';' in
>>/var/www/html/lc.php on line 44
>>
>>line 44 is var $PictureList[] = new C_PictureList();
>>
>
>
> You should assign $PictureList in the constructor or with a setter:
>
> class C_Category {
> var $PictureList = array();
>
> function C_Category() {
> $this->PictureList[] = new C_PictureList();
> }
> }
>
>
> JW
>
>
>
PictureList[] is supposed to be an array of C_PictureList objects, not a
member of the C_PictureList class.

Re: sytax error, but where?

am 30.10.2005 12:19:22 von Janwillem Borleffs

carlton wrote:
> I added the var to line 44, and now get the error
> Parse error: parse error, unexpected '[', expecting ',' or ';' in
> /var/www/html/lc.php on line 44
>
> line 44 is var $PictureList[] = new C_PictureList();
>

You should assign $PictureList in the constructor or with a setter:

class C_Category {
var $PictureList = array();

function C_Category() {
$this->PictureList[] = new C_PictureList();
}
}


JW

Re: sytax error, but where?

am 30.10.2005 13:49:48 von Janwillem Borleffs

carlton wrote:
> PictureList[] is supposed to be an array of C_PictureList objects,
> not a member of the C_PictureList class.
>

You are defining PictureList in the class, which makes it a class member or
a property at least, regardless of what it contains.

Therefore, you are bounded to some restrictions regarding the syntax. If you
don't want it to be member of the class, but accessible through it, one
option would be to define it as a static variable within a class method:


class C_PictureList {}

class C_Category {
function accessPictureList($new_value = null) {
static $PictureList = array();
if (is_object($new_value)) {
$PictureList[] = $new_value;
} else {
return $PictureList;
}
}
}

// Set
C_Category::accessPictureList(new C_PictureList);

// Get
print_r(C_Category::accessPictureList());

?>

JW