Skip to content

Instantly share code, notes, and snippets.

@reaneyk
Created September 18, 2013 04:39
Show Gist options
  • Save reaneyk/6604607 to your computer and use it in GitHub Desktop.
Save reaneyk/6604607 to your computer and use it in GitHub Desktop.
Example PHP Interface class
<?php
//Define a new Interface for all 'shapes' to inherit
interface Shape {
//Define the methods required for classes to implement
public function getColor();
public function setColor($color);
//Interfaces can't define common functions so we'll just
//define a method for all implementations to define
public function describe();
}
//Define a new 'Triangle' class that inherits from the
//'Shape' interface
class Triangle implements Shape {
private $color = null;
//Define the required methods defined in the abstract
//class 'Shape'
public function getColor() {
return $this->color;
}
//Note that the method signature matches the abstract
// class with only one parameter
public function setColor($color) {
$this->color = $color;
}
//Interface can't have common functions so they must be defined
//within the child class
public function describe() {
return sprintf("I'm an %s %s\n", $this->getColor(), get_class($this));
}
}
//Instantiate the Triange class
$triangle = new Triangle();
//Set the color
$triangle->setColor('Orange');
//Print out the value of the describe common method
//provided by the abstract class
//Will print out out "I'm an Orange Triange"
print $triangle->describe();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment