Created
July 9, 2010 21:29
-
-
Save jlengstorf/470093 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class TrainRider | |
{ | |
static $num_riders = 0; | |
public $times_ridden = 0; | |
public function rideTrain() | |
{ | |
++$this->times_ridden; | |
++self::$num_riders; | |
} | |
} | |
// Create two new train riders | |
$tom = new TrainRider; | |
$john = new TrainRider; | |
$tom->rideTrain(); // Go to work | |
$tom->rideTrain(); // Come home | |
$john->rideTrain(); // Go to his parents' house for the weekend | |
// Output information about the train and its riders | |
echo "Tom has ridden the train ", $tom->times_ridden, " times.<br />", | |
"John has ridden the train ", $john->times_ridden, " times.<br />", | |
"The train has had a total of ", TrainRider::$num_riders, " riders."; | |
// Output is: | |
// Tom has ridden the train 2 times. | |
// John has ridden the train 1 times. | |
// The train has had a total of 3 riders. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment