How to declare class fields?


class Interface
{
	// current group's ID
	private $groupID = -1;
}


Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /homepages/38/d167792529/htdocs/temp/Interface.php on line 13

An error is reported on my class field declaration. I followed a similar layout from http://us.php.net/manual/en/language.oop5.visibility.php . What’s wrong?

Interface is a reserved word in PHP, if I’m not mistaken.

I’ve changed it to “Layout” and I still get the same problem.

Well, I’d then ask, what’s line 13?

I was able to supress the error by changing “private” to “var”, but I would prefer it to be private…

Line 13 is that class field declaration. This is the upper half of my class:


<?php

// title:   Interface.php
// author:  ---
// date:    05/14/08
// purpose: Reused interface elements are put in functions to avoid redundant
//          typing and to allow easy updatability. 
// ============================================================================ 

class Layout
{
	// current group's ID
	private $groupID = -1;
	
	// ========================================================================
	// Interface
	//    args:  int: ID number of the current group
	//    ret:   none
	//    about: constructor
	function __construct($id)
	{
		$this->groupID = $id;
	}

I also realized that the above constructor does not ever get called. So I tried this instead:


class Layout
{
	var $groupID;
	
	function Layout($id)
	{
		$this->groupID = $id;
		echo 'id: ' . $id . ', ' . 'groupID: ' . $this->$groupID . '<br />';
	}
}

I called it with:


$layout = new Layout(3);

and the output was:

id: 3, groupID: 

So the class field was never set…

In your constructor, where you set the value of $this->$groupID, it should be $this->groupID. You only use the dollar sign on the $this pseudo-variable.

Also, in setting the visibility, I think you can only do that in php 5.

Indeed visibility settings where implemented in PHP 5.
and constructers too!