# How to declare class fields?

**URL:** <https://forum.kirupa.com/t/how-to-declare-class-fields/260363>\
**Category:** programming\
**Created:** [May 14, 2008, 7:35pm UTC](https://forum.kirupa.com/t/how-to-declare-class-fields/260363 "2008-05-14T19:35:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![NeoDreamer](https://avatars.discourse-cdn.com/v4/letter/n/ccd318/32.png) [@NeoDreamer](https://forum.kirupa.com/u/NeoDreamer)\
**Post date:** [May 14, 2008, 7:35pm UTC](https://forum.kirupa.com/t/how-to-declare-class-fields/260363/1 "2008-05-14T19:35:53Z")

</div>

```php

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

```

```auto

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](http://us.php.net/manual/en/language.oop5.visibility.php) . What’s wrong?

---

<div class="post-metadata">

**Author:** ![kdd](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kdd](https://forum.kirupa.com/u/kdd)\
**Post date:** [May 14, 2008, 8:33pm UTC](https://forum.kirupa.com/t/how-to-declare-class-fields/260363/2 "2008-05-14T20:33:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![NeoDreamer](https://avatars.discourse-cdn.com/v4/letter/n/ccd318/32.png) [@NeoDreamer](https://forum.kirupa.com/u/NeoDreamer)\
**Post date:** [May 14, 2008, 9:10pm UTC](https://forum.kirupa.com/t/how-to-declare-class-fields/260363/3 "2008-05-14T21:10:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kdd](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kdd](https://forum.kirupa.com/u/kdd)\
**Post date:** [May 14, 2008, 9:30pm UTC](https://forum.kirupa.com/t/how-to-declare-class-fields/260363/4 "2008-05-14T21:30:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![NeoDreamer](https://avatars.discourse-cdn.com/v4/letter/n/ccd318/32.png) [@NeoDreamer](https://forum.kirupa.com/u/NeoDreamer)\
**Post date:** [May 14, 2008, 10:06pm UTC](https://forum.kirupa.com/t/how-to-declare-class-fields/260363/5 "2008-05-14T22:06:28Z")

</div>

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

<?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;
	}

```

---

<div class="post-metadata">

**Author:** ![NeoDreamer](https://avatars.discourse-cdn.com/v4/letter/n/ccd318/32.png) [@NeoDreamer](https://forum.kirupa.com/u/NeoDreamer)\
**Post date:** [May 14, 2008, 10:24pm UTC](https://forum.kirupa.com/t/how-to-declare-class-fields/260363/6 "2008-05-14T22:24:30Z")

</div>

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

```php

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

```

I called it with:

```php

$layout = new Layout(3);

```

and the output was:

```auto
id: 3, groupID: 

```

So the class field was never set…

---

<div class="post-metadata">

**Author:** ![djheru](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/djheru/32/3190_2.png) [@djheru](https://forum.kirupa.com/u/djheru)\
**Post date:** [May 15, 2008, 7:35am UTC](https://forum.kirupa.com/t/how-to-declare-class-fields/260363/7 "2008-05-15T07:35:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![borrob](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@borrob](https://forum.kirupa.com/u/borrob)\
**Post date:** [May 15, 2008, 12:20pm UTC](https://forum.kirupa.com/t/how-to-declare-class-fields/260363/8 "2008-05-15T12:20:46Z")

</div>

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