I have this, that doesnt work obviously.
$start = $hours->start{$day};
I need to be able the change the $day dynamically. $day can be any day of the week
//For example
$start = $hours->startSunday;
Let me try to be more clear. The following objects contain a certain time
// will echo something like 8:00am
echo $hours->startSunday;
//will echo something like 7:00am
echo $hours->startMonday;
I need to be able to change the day part dynamically with a variable, since it can be any day of the week.
//so something like
echo $hours->start.$day;
but that doesnt work
First. You could edit syntax with
$hours = new stdClass();
$day = 'Sunday';
$hours->{'start'.$day} = 10;
$start = $hours->{'start'.$day};
var_dump($start);
Secnod. Better ot use getter and setter methods.
class Hours
{
private $hours = array();
public function getStart($day)
{
return $this->hours[$day];
}
public function setStart($day, $value)
{
$this->hours[$day] = $value;
}
}
$hours = new Hours();
$day = 'Sunday';
$hours->setStart($day, 10);
$start = $hours->getStart($day);
var_dump($start);
You can use magic method, __get() and __set()
class Hours {
private $data = array();
public function __get($day) {
return $this->data[$day];
}
public function __set($day, $val) {
$this->data[$day] = $val;
}
}
$h = new Hours();
echo $hours->startMonday;