I'm looking for a way of a single CLASS named "Bear" to be able to:
- Eat honey every 2 hours and remember when they last had honey.
- Decide if they need to sleep.
I want to know is there a way of doing this without hard coding the array times and rather than using an else statement to decide if the bear needs to sleep would a separate function be better and cleaner?
class Bear
{
protected $name;
protected $currentTime;
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
public function showLastFeedtime()
{
$lastFed = strtotime('-2 hour', strtotime($this->currentTime));
$newdate = date('H:i', $lastFed);
echo $this->name." last ate at ".$newdate."<br/><br/>";
}
public function checkFeedingTime($checkHours = array())
{
if(in_array($this->currentTime,$checkHours))
{
echo "It's ".$this->name."'s feeding time </br>";
echo "The time ".$this->currentTime. " is the time and I'm eating
honey at right now. </br></br>";
echo "These are the times I eat</br>";
foreach($checkHours as $feedingTimes)
{
echo $feedingTimes."</br>";
}
echo "<hr>";
}
else
{
//DECIDE IF THE BEAR NEEDS TO SLEEP
echo "Barney is having a snooze";
}
}
public function getTime($currentTime)
{
$this->currentTime = $currentTime;
}
public function setTime()
{
return $this->currentTime;
}
}
//new instance of barney
$barney = new Bear();
//set the bears name
$barney->setName('Barney');
//get the bears name
$bigBear = $barney->getName();
//page info
echo "<h1>".$bigBear."'s profile page </h1>";
echo "<hr>";
echo "I am a bear and my name is {$bigBear} <br/>";
echo "<hr>";
//the current time
$currentTime = date('H:i');
$barney->getTime($currentTime);
//last feeding time
$barney->showLastFeedtime();
//ARRAY TIMES-barney eats every 2 hours
$checkHours =
array('00:00','02:00','04:00','06:00','08:00',
'10:00','12:00','14:00','16:00','18:00','20:00','22:00');
//call the feeding time function
$barney->checkFeedingTime($checkHours);
?>