Did we talk about why we declare properties in a class with
the
visibility
private?
Answer; To protect the integrity of the properties. We, the authors
of the class, know better how to do it. We don't want any user
(program) to interfere in that, thus:
<?php
class Runner {
private $name; // name
private $club; // affiliation
private $distance; // race distance
private $stride; // length of each step in cms
private $cadence; // number of steps per minute
public function __construct($name, $club, $distance, $stride, $cadence) {
$this->name = $name;
$this->club = $club;
$this->distance = $distance;
$this->stride = $stride;
$this->cadence = $cadence;
}
public function getStride() {
return $this->stride;
}
public function setStride($stride) {
$this->stride = $stride;
}
public function getRunner() {
return $this->name . ", " . $this->club;
}
public function runSpeed() {
return $this->stride * $this->cadence / 100 * 60 / 1000; // km/h
}
}
?>
Exercise: Test the above class!
The normal procedure for a user is to ascertain that arguments sent into methods (or functions) are of the right type. In a dynamically typed language, this, of course, is a run-time issue, not a compile-time problem. PHP offers you a series of static functions to do just that:
<?php
bool is_bool(mixed $var) // returns boolean, param mixed type
// usage
if (is_boo($var)) {
echo "True";
} else {
echo "False";
}
is_integer($var); // returns true if $var is integer
is_double($var); // returns true if $var is double
is_string($var); // returns true if $var is character data
is_object($var); // returns true if $var is an object ref
is_array($var); // returns true if $var is an array
is_resource($var); // returns true if $var is a resource (file or db)
is_null($var); // returns true if $var has no value
Now take the Runner from above. let us
imagine that sometimes we need to gather runners for seminars
and lectures, not necvessarily for races. The class Runner then seems
quite ill suited. We need essentials, but not all.
Let's look at a class diagram ... draw on blackboard
Then we shall code the corresponding (new) classes
<?php
class Runner {
private $name; // name
private $club; // affiliation
public function __construct($name, $club) {
$this->name = $name;
$this->club = $club;
}
public function getRunner() {
return $this->name . ", " . $this->club;
}
}
?>
The runner reduced to the bare person-related stuff, but now look. The RaceRunner IS_A Runner
<?php
class RaceRunner extends Runner {
private $distance; // race distance
private $stride; // length of each step in cms
private $cadence; // number of steps per minute
public function __construct($name, $club, $distance, $stride, $cadence) {
parent::__construct($name, $club);
$this->distance = $distance;
$this->stride = $stride;
$this->cadence = $cadence;
}
public function getStride() {
return $this->stride;
}
public function setStride($stride) {
$this->stride = $stride;
}
public function runSpeed() {
return $this->stride * $this->cadence / 100 * 60 / 1000; // km/h
}
}
?>
Usage:
<?php
...
include "Runner.inc.php";
include "RaceRunner.inc.php";
...
...
$seminarParticipant = new Runner("Niels", "AGF"); // runner only, not connected to race
printf("<br />%s\n", $seminarParticipant->getRunner());
...
...
$racer = new RaceRunner("Niels", "AGF", 12, 120, 180);
printf("<td>%s"</td><td>%s"</td>\n", $racer->getRunner(), $racer->runSpeed();
?>
Exercise: Complete the usage example, to demonstrate that it works.