The line:
$this->display = " $this->n"."$this->suf"." $this->name";
is the first line of the class’ constructor. It stores in the $display
property of the object a string that contains only spaces because the values it contains are not set yet.
Read about double-quotes strings and variables parsing inside double-quotes strings.
In order to work, the class time_value
should be like this:
class time_value
{
private $t, $name, $display;
public function __construct($t, $name)
{
$this->t = $t;
$this->name = $name;
$n = date($this->t, time());
switch ($n)
{
case 1:
$suf="st";
break;
case 2:
$suf="nd";
break;
case 3:
$suf="rd";
break;
default:
$suf="th";
break;
}
$this->display = " {$n}{$suf} {$this->name}";
}
public function display() { return $this->display; }
}
$sec = new time_value('s', 'seconds');
$min = new time_value('i', 'minutes');
// all the other time components here...
echo $sec->display().$o.$min->display(); // ...
The next step toward object-oriented programming is to encapsulate the generation of all time components into the time_value
class (or into another class that uses time_value
instances, if you like it more) and have the code of function say_time()
look like this:
function say_time()
{
$time = new time_value();
echo "You are, at this moment, living in the ".$time->display("of the")." since the begining of the International calender.";
}
solved Time display function not working