Forum Moderators: coopster

Message Too Old, No Replies

PHP 4 OO issue

unexpected T_VARIABLE, expecting T_FUNCTION

         

ryan_b83

8:56 pm on Mar 20, 2007 (gmt 0)

10+ Year Member



Hello, I have this simple script that i have a 3 dimensional array. However I am kinda new to OO php, and it keep getting the same error? Any ideas?

class maps{

//Initalize the city array
var $City = array();

$City['city1']['Dundas']['Codes'] = "L9H, L85"; //ERROR LINE
$City['city1']['Dundas']['Amount'] = 13915;

$City['city2']['North Burlington']['Codes'] = "L7M, L7P";
$City['city2']['North Burlington']['Amount'] = 19668;

$City['city3']['South Oakville']['Codes'] = "L6K, L6L, L6M";
$City['city3']['South Oakville']['Amount'] = 26044;

function display_cities(){
echo "hi";
foreach($this->City as $kCity=>$vCity){
echo $kCity." = ".$vCity."<br>";
}
echo "bye";
}
}

Error is in this class file on the line noted.
Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION

This is probably really simple, but i've been looking at it for a couple hours and cant figure it out. Thanks,
Ryan

coopster

10:20 pm on Mar 20, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Remember that a class is a collection of variables and functions working with these variables. The error you are receiving is because you are working on the variables outside of a function. In PHP4 you can create a function (method) with the same name as the class and it will be the constructor (automagically executed) when the class is instantiated.
<?php 
class maps{
//Initalize the city array
var $City = array();
function maps()
{
$this->City['city1']['Dundas']['Codes'] = "L9H, L85"; //ERROR LINE
$this->City['city1']['Dundas']['Amount'] = 13915;
$this->City['city2']['North Burlington']['Codes'] = "L7M, L7P";
$this->City['city2']['North Burlington']['Amount'] = 19668;
$this->City['city3']['South Oakville']['Codes'] = "L6K, L6L, L6M";
$this->City['city3']['South Oakville']['Amount'] = 26044;
$this->display_cities(); // execute this class method
}
function display_cities()
{
echo "hi";
foreach($this->City as $kCity=>$vCity){
echo $kCity." = ".$vCity."<br>";
}
echo "bye";
}
}
$map = new maps();
exit;
?>

ryan_b83

1:36 pm on Mar 21, 2007 (gmt 0)

10+ Year Member



Awesome, that worked. Thanks alot!