Skip to content

Instantly share code, notes, and snippets.

@jlengstorf
Created July 9, 2010 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlengstorf/470093 to your computer and use it in GitHub Desktop.
Save jlengstorf/470093 to your computer and use it in GitHub Desktop.
<?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