note 108736 added to language.oop5.basic
| From: | Jeffrey | Date: | Mon, 21 May 2012 22:36:19 +0000 |
| Subject: | note 108736 added to language.oop5.basic | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-188121@lists.php.net to get a copy of this message | ||
The PHYSICS OF THE SYSTEM
You cannot take the PHYSICS out of any SYSTEM (or environment) -- man-made OR natural. In every
system, there lies the laws of physics (the laws of nature). For example, a moving object -- in a
natural environment OR in a lab -- is always subject to the same forces that exist anywhere. Even a
computer program has an environment, for example:
<?php
?>
Even though it had a very short life, it had an ENVIRONMENT which had POTENTIAL TO DO WORK, which
made it a good candidate for a useful SYSTEM. If we think about a 3-Dimensional space as an
environment, it is easy to imagine putting 'things' or 'objects' in that space
AND doing things to them.
In the next invocation of PHP below, I am going to:
1. Create a 3-Dimensional space or ENVIRONMENT -- just by using the '<?php' tag.
2. Define the SYSTEM we want to use -- laws, rules, policies, structure, properties etc. -- with
valid PHP class definitions.
3. Instantiate objects with the 'new' operator so we can poke it for a while.
4. Output some data to see if they obey the rules we wrote.
5. Here we go!
<?php
// 1. check, ENVIRONMENT created.
class Wheel
{
public $cir = 3.0; // circumference in feet.
public $rpm = 0.0; // rotations (spins) per minute.
public function __toString()
{
return 'Wheel [' .
'cir=' . number_format($this->cir, 2) . ' ft, ' .
'rpm=' . $this->rpm .
']';
}
}
// Wheel defined...
class Speedometer
{
public $wheel;
public function __construct(Wheel $wheel)
{
$this->wheel = $wheel;
}
public function getSpeed()
{
$speed = 0.0;
$wheel = $this->wheel;
if($wheel->rpm != 0.0)
{
$fpm = $wheel->cir * $wheel->rpm; // feet per minute
$fph = $fpm * 60; // feet per hour
$speed = $fph / 5280; // MPH = $fph / feet per mile
}
return $speed;
}
public function __toString()
{
return 'Speedometer [' .
'speed=' . number_format($this->getSpeed(), 2) . ' MPH, ' .
'wheel=' . $this->wheel .
']';
}
}
// Speedometer defined.
// 2. check, SYSTEM created.
$wheel = new Wheel();
$speedometer = new Speedometer($wheel);
// 3. check, Objects instantiated.
while($wheel->rpm <= 1500)
{
echo $speedometer . "<br>\n";
$wheel->rpm += 100;
}
// 4. check, data outputted.
?>
Speedometer [speed=0.00 MPH, wheel=Wheel [cir=3.00 ft, rpm=0]]
Speedometer [speed=3.41 MPH, wheel=Wheel [cir=3.00 ft, rpm=100]]
Speedometer [speed=6.82 MPH, wheel=Wheel [cir=3.00 ft, rpm=200]]
... etc ...
... etc ...
Speedometer [speed=51.14 MPH, wheel=Wheel [cir=3.00 ft, rpm=1500]]
5. check, there it went.
----
Server IP: 69.147.83.197
Probable Submitter: 66.81.159.93
----
Manual Page -- http://www.php.net/manual/en/language.oop5.basic.php
Edit -- https://master.php.net/note/edit/108736
Del: integrated -- https://master.php.net/note/delete/108736/integrated
Del: useless -- https://master.php.net/note/delete/108736/useless
Del: bad code -- https://master.php.net/note/delete/108736/bad+code
Del: spam -- https://master.php.net/note/delete/108736/spam
Del: non-english -- https://master.php.net/note/delete/108736/non-english
Del: in docs -- https://master.php.net/note/delete/108736/in+docs
Del: other reasons-- https://master.php.net/note/delete/108736
Reject -- https://master.php.net/note/reject/108736
Search -- https://master.php.net/manage/user-notes.php