I have a method in a class that updates one of 7 variables/properties in the class. I would like to reduce the "switch" command to one line by using a variable variable, but I don't seem to be able to make it work.
The value passed to the function/method is the day of the week "Sunday", "Monday", ...
My sample code below returns "variable undefined" on the $$thisSubTotal += $cVal;
line
The php manual explains it as this:
This explanation does not make any sense to me, so this is where I need help.
The value passed to the function/method is the day of the week "Sunday", "Monday", ...
My sample code below returns "variable undefined" on the $$thisSubTotal += $cVal;
line
The php manual explains it as this:
Class properties may also be accessed using variable property names. The variable property name will be resolved within the scope from which the call is made. For instance, if you have an expression such as $foo->$bar, then the local scope will be examined for $bar and its value will be used as the name of the property of $foo. This is also true if $bar is an array access.
This explanation does not make any sense to me, so this is where I need help.
Code:
class reportFunctions
{
public $cSun = 0.00;
public $cMon = 0.00;
public $cTue = 0.00;
public $cWed = 0.00;
public $cThu = 0.00;
public $cFri = 0.00;
public $cSat = 0.00;
function dayDetail($thisDay)
{
$cVal = 1;
$shortDay = array('Sunday'=>'cSun','Monday'=>'cMon','Tuesday'=>'cTue','Wednesday'=>'cWed','Thursday'=>'cThu','Friday'=>'cFri','Saturday'=>'cSat');
$thisSubTotal = '$this->'.$shortDay[$thisDay];
[B]//how can one of the day variables be updated using variable variable here
$$thisSubTotal += $cVal;[/B]
switch ($thisDay)
{
case 'Sunday':
$this->cSun += $cVal;
break;
case 'Monday':
$this->cMon += $cVal;
break;
case 'Tuesday':
$this->cTue += $cVal;
break;
case 'Wednesday':
$this->cWed += $cVal;
break;
case 'Thursday':
$this->cThu += $cVal;
break;
case 'Friday':
$this->cFri += $cVal;
break;
case 'Saturday':
$this->cSat += $cVal;
break;
}
}
}
Comment