01-30-2008, 11:59 AM
|
#3 (permalink)
|
|
The Frequenter
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
|
Hi Sarmenhb,
I've re-written your class a little bit in the hope it will make more sense. Also, I've put lots of comments along with a working example at the bottom so hopefully it will help you understand
PHP Code:
<?php
// We start by declaring our fetchTime class
Class fetchTime
{
// Properties (variables) to hold our hours, minutes and seconds
// We will set these to 'private' to force people to use our getHour(),
// getMinute() and getSecond() methods that we create below
private $hour = 0;
private $minute = 0;
private $second = 0;
// This is a magic PHP function that is run each time our class is run
// We will use it to format our timestamp and populate the $hour,
// $minute and $second properties
public function __construct($timestamp = 0)
{
// Convert our timestamp into the format of hours:minutes:seconds
$time = date('h:i:s', $timestamp);
// Now we have to break apart our $time variable to it's parts
$timeParts = explode(':', $time);
// And finally, we set our $hour, $minute and $second properties
// using our $timeParts array
$this->hour = $timeParts[0];
$this->minute = $timeParts[1];
$this->second = $timeParts[2];
}
// The getHour() method (function)
// This will return the Hour ($this->hour)
public function getHour()
{
return $this->hour;
}
// The getMinute() method
// This will reutrn the Minute ($this->minute)
public function getMinute()
{
return $this->minute;
}
// The getSecond() method
// This will return the Second ($this->second)
public function getSecond()
{
return $this->second;
}
}
// -----------------------------------------------------
// Test Code
// It's important to note that this example / test code
// would probably be in another file - it is generally
// bad practice to put anything other than the class
// in a class file
// -----------------------------------------------------
// Create a new instance of our fetchTime() class and put it in $timeObject variable
// We use the time() function to provide our class with a unix timestamp
$timeObject = new fetchTime(time());
// Echo our the Hour, Minute and Seconds
echo 'Hour: ' . $timeObject->getHour() . '<br />';
echo 'Minute: ' . $timeObject->getMinute() . '<br />';
echo 'Second: ' . $timeObject->getSecond() . '<br />';
If there are any parts you don't understand or are unsure about, just ask
Alan
|
|
|