Skip to content

Instantly share code, notes, and snippets.

@earth3300
Created August 23, 2018 16:02
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 earth3300/9b71ed216af960200f8525eacf738a08 to your computer and use it in GitHub Desktop.
Save earth3300/9b71ed216af960200f8525eacf738a08 to your computer and use it in GitHub Desktop.
<?php
defined( 'NDA' ) || exit('Access Denied.');
/**
* Pattern Abstract Class Function
*
* @link http://www.primitivetype.com/articles/php_5_abstract_classes_and_methods.php
*/
/**
* Abstract Class Vehicle.
*/
abstract class Vehicle
{
/**
* Public abstract function Gets the Number of Wheels
*/
public abstract function getNumWheels();
/**
* Public function Gets the Name
*/
public function getName()
{
return get_class($this);
}
}
/**
* Class Car Extends Vehicle.
*/
class Car extends Vehicle
{
/**
* Public Function Gets the Number of Wheels.
*/
public function getNumWheels()
{
return 4;
}
}
/**
* Class Bike Extends Vehicle.
*/
class Bike extends Vehicle {
/**
* Public Function Gets the Number of Wheels.
*/
public function getNumWheels()
{
return 2;
}
}
/**
* Prints (echos) the number of wheels.
*/
function printNumWheels(Vehicle $v)
{
echo "A " . $v->getName() . " has " . $v->getNumWheels() . " wheels\n";
}
$car = new Car();
$bike = new Bike();
printNumWheels($car);
printNumWheels($bike);
/*
output:
A Car has 4 wheels
A Bike has 2 wheels
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment