Skip to content

Instantly share code, notes, and snippets.

@wchan2
Last active August 29, 2015 14:10
Show Gist options
  • Save wchan2/999fb0081577335bc124 to your computer and use it in GitHub Desktop.
Save wchan2/999fb0081577335bc124 to your computer and use it in GitHub Desktop.
Baruch PHP & MySQL Course

Baruch's PHP and MySQL Class Example Code

Due to a popular demand for object-oriented PHP, here are the examples that I covered in class.

Files included:

  • vehicle.php - a generic class that has properties and methods that any "vehicle" can extend
  • car.php - a car class that extends vehicle
  • test.php - requires vehicle.php and car.php and extends a series of statements with comments that represents what should be outputted

Credits:

Written by Will Chan
Homepage: http://www.chanify.com
Original Source: https://github.com/wchan2/baruch_php_examples

<?php
/**
* Car - inherits from the vehicle class
* @package default
* @author William Chan
*/
class Car extends Vehicle {
protected $brand;
protected $gallons_of_gas;
// Constructor to create the car object
public function __construct($num_wheels = 4, $speed = 0, $init_gas = 0, $brand = 'Honda') {
parent::__construct($num_wheels, $speed);
$this->brand = $brand;
$this->gallons_of_gas = $init_gas;
}
// Adds the specified number of gallons of gas
public function add_gas($gallons_to_add) {
$this->gallons_of_gas += $gallons_to_add;
}
// Drive - for each hour I drive I lose 10 gallons of gas
public function drive($hours_drove) {
$this->gallons_of_gas -= 10 * $hours_drove;
if ($this->gallons_of_gas < 0) {
$this->gallons_of_gas = 0;
print $this->breakdown();
}
}
// Gets the remaining gas left
public function gas_left() {
return $this->gallons_of_gas;
}
// Returns the brand of the car
public function get_brand() {
return $this->brand;
}
// Trade in your car
public function trade_in_for($brand = 'BMW') {
$this->brand = $brand;
}
// Returns the statement that the car has broken down
private function breakdown() {
return "My car broke! :( :(";
}
}
?>
<?php
require_once 'vehicle.php';
require_once 'car.php';
/**
* Test code for Vehicle
*/
$wills_vehicle = new Vehicle(2, 20);
print "Will's vehicle has ".$wills_vehicle->get_speed()." horsepower.\n\n"; // Will's vehicle has 20 horsepower.
print "Will's vehicle has ".$wills_vehicle->get_num_wheels()." wheels.\n\n"; // Will's vehicle has 4 wheels.
/**
* Test code for Car
*/
print "Will's going to get a car now!\n\n";
$wills_car = new Car(4, 100, 10, 'Toyota');
print "Will's car is a ".$wills_car->get_brand().".\n\n"; // Will's car is a Toyota
print "Will's car has ".$wills_car->gas_left()." gallons of gas left.\n\n"; // Will's car has 10 gallons of gas left.
print "Adding 100 gallons of gas...\n\n";
$wills_car->add_gas(100);
print "How many gallons of gas does Will's car have now?\t\t".$wills_car->gas_left(); // 110
$wills_car->drive(5); // 110 - (5 * 60)
print "\n\nWhat about now?\t\t".$wills_car->gas_left()."\n\n";
// Driving too much...
$wills_car->drive(100); // My car broke! :( :(
print "\n\nOh well. Time to trade it in for a new one!\n\n";
$wills_car->trade_in_for("Porsche");
print $wills_car->get_brand()." goes VROOM!!!";
?>
<?php
/**
* Vehicle
*
* @package default
* @author William Chan
*/
class Vehicle {
// Properties of the vehicle object
protected $num_wheels;
protected $speed;
// Constructor to create the vehicle object
public function __construct($num_wheels = 0, $speed = 0) {
$this->num_wheels = $num_wheels;
$this->speed = $speed;
}
// Returns the speed of the vehicle
public function get_speed() {
return $this->speed;
}
// Returns the number of wheels on the vehicle
public function get_num_wheels() {
return $this->num_wheels;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment