Click to See Complete Forum and Search --> : Class Contains Array
bubbisthedog
01-29-2007, 08:48 PM
I am very new to OOP in PHP (not OOP in general) and I'm getting an error (Parse error: parse error, unexpected T_VARIABLE, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}') when I try to access the following class:
class DateOptions
{
var $WhatMonth;
var $Month;
$Month[1] = 'jan';
$Month[2] = 'feb';
$Month[3] = 'mar';
$Month[4] = 'apr';
$Month[5] = 'may';
$Month[6] = 'jun';
$Month[7] = 'jul';
$Month[8] = 'aug';
$Month[9] = 'sep';
$Month[10] = 'oct';
$Month[11] = 'nov';
$Month[12] = 'dec';
function buildDateOptions($AgainstWhat)
{
for ($WhatMonth=1; $WhatMonth<=12; $WhatMonth++)
{
echo ('<option value="'.$WhatMonth.'"');
if ($WhatMonth == $AgainstWhat)
{
echo (' selected=\"selected\"');
}
echo ('>'.$this->Month[$WhatMonth].'</option>');
}
}
}
Here's how I'm trying to access the function within the class:
$BegAvailDate = new DateOptions;
$BegAvailDate->buildDateOptions($EAVValue[0]);
Is there something wrong with the way that I've put the array in the class?
Any assistance would be greatly appreciated. I am a quick study once I learn some fundamentals. :cool:
bubbisthedog
NogDog
01-29-2007, 09:05 PM
I believe you have to instantiate an object with "()" after the class name, even if you do not have a constructor method that accepts arguments:
$BegAvailDate = new DateOptions();
bubbisthedog
01-29-2007, 09:14 PM
I believe you have to instantiate an object with "()" after the class name, even if you do not have a constructor method that accepts arguments:
$BegAvailDate = new DateOptions();
Thanks for the help, NogDog. Unfortunately, I am getting the same error when I do that. Shame on me for not mentioning earlier that the error is saying, at the end, 'on line [line number of first array comparison {in this case $Month[1] = 'jan';}]'. Perhaps that gives more insight?
Thanks again for taking the time to help me,
bubbisthedog
bubbisthedog
01-29-2007, 09:19 PM
I changed the array to this, though it didn't make the error go away:
$this->Month[1] = 'jan';
$this->Month[2] = 'feb';
$this->Month[3] = 'mar';
$this->Month[4] = 'apr';
$this->Month[5] = 'may';
$this->Month[6] = 'jun';
$this->Month[7] = 'jul';
$this->Month[8] = 'aug';
$this->Month[9] = 'sep';
$this->Month[10] = 'oct';
$this->Month[11] = 'nov';
$this->Month[12] = 'dec';
Perhaps this is at least the correct syntax.
SpectreReturns
01-29-2007, 09:26 PM
Your problem is that you can't initialize the array in the root of the class. Move the $this->Month[] things into the constructor and it will work.
pcthug
01-29-2007, 09:34 PM
class DateOptions
{
var $WhatMonth;
var $Month;
function DateOptions
{
$this->Month[1] = 'jan';
$this->Month[2] = 'feb';
$this->Month[3] = 'mar';
$this->Month[4] = 'apr';
$this->Month[5] = 'may';
$this->Month[6] = 'jun';
$this->Month[7] = 'jul';
$this->Month[8] = 'aug';
$this->Month[9] = 'sep';
$this->Month[10] = 'oct';
$this->Month[11] = 'nov';
$this->Month[12] = 'dec';
}
function buildDateOptions($AgainstWhat)
{
for ($WhatMonth=1; $WhatMonth<=12; $WhatMonth++)
{
echo ('<option value="'.$WhatMonth.'"');
if ($WhatMonth == $AgainstWhat)
{
echo (' selected=\"selected\"');
}
echo ('>'.$this->Month[$WhatMonth].'</option>');
}
}
}
bubbisthedog
01-29-2007, 09:38 PM
Your problem is that you can't initialize the array in the root of the class. Move the $this->Month[] things into the constructor and it will work.
Bah-dah-bing. Added the constructor and it works perfectly. Thanks, Spectre.:D Question: Is my syntax, for the most part, correct in my class now? As a complete newbie to OOP in PHP, I'd greatly appreciate any critique that you (or anyone else) can provide.
class DateOptions
{
var $WhatMonth;
var $Month;
function DateOptions()
{
$this->Month[1] = 'jan';
$this->Month[2] = 'feb';
$this->Month[3] = 'mar';
$this->Month[4] = 'apr';
$this->Month[5] = 'may';
$this->Month[6] = 'jun';
$this->Month[7] = 'jul';
$this->Month[8] = 'aug';
$this->Month[9] = 'sep';
$this->Month[10] = 'oct';
$this->Month[11] = 'nov';
$this->Month[12] = 'dec';
}
function buildDateOptions($AgainstWhat)
{
for ($this->WhatMonth=1; $this->WhatMonth<=12; $this->WhatMonth++)
{
echo ('<option value="'.$this->WhatMonth.'"');
if ($this->WhatMonth == $AgainstWhat)
{
echo (' selected=\"selected\"');
}
echo ('>'.$this->Month[$this->WhatMonth].'</option>');
}
}
}
SpectreReturns
01-29-2007, 09:39 PM
That would work, but easier on the class engine as:
class DateOptions
{
var $WhatMonth;
var $Month = array(1 => 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec');
function buildDateOptions($AgainstWhat)
{
for ($WhatMonth=1; $WhatMonth<=12; $WhatMonth++)
{
echo ('<option value="'.$WhatMonth.'"');
if ($WhatMonth == $AgainstWhat)
{
echo (' selected=\"selected\"');
}
echo ('>'.$this->Month[$WhatMonth].'</option>');
}
}
}
bubbisthedog
01-29-2007, 09:40 PM
And thanks, pcthug, for showing me exactly what to do! (Believe it or not, I actually figured that out by myself! :eek: )
bubbisthedog
01-29-2007, 09:43 PM
That would work, but easier on the class engine as:
class DateOptions
{
var $WhatMonth;
var $Month = array(1 => 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec');
function buildDateOptions($AgainstWhat)
{
for ($WhatMonth=1; $WhatMonth<=12; $WhatMonth++)
{
echo ('<option value="'.$WhatMonth.'"');
if ($WhatMonth == $AgainstWhat)
{
echo (' selected=\"selected\"');
}
echo ('>'.$this->Month[$WhatMonth].'</option>');
}
}
}
NICE. Thanks so much for the suggestion, Spectre! I'll do it that way. Hey, I'm a little confused on when to use $this->; any way you could de-confuse me on that?