Skip to content

Instantly share code, notes, and snippets.

@technoknol
Created September 3, 2017 18:41
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 technoknol/11b898de852fc9fa7bae7935e95e7b54 to your computer and use it in GitHub Desktop.
Save technoknol/11b898de852fc9fa7bae7935e95e7b54 to your computer and use it in GitHub Desktop.
Type hinting with Interfaces PHP - Example 3
<?php
// output :
// 10
// 100.234
interface printable {
public function printme();
}
abstract class Number {
private $value;
abstract public function value();
public function reset() {
$this->value = NULL;
}
}
class Integer extends Number implements printable {
private $value;
function __construct($value) {
$this->value = $value;
}
function value() {
return $this->value;
}
public function getValue() {
return (int)$this->value;
}
public function printme() {
echo (int)$this->value;
}
}
class Floats extends Number implements printable {
private $value;
function __construct($value) {
$this->value = $value;
}
function value() {
return $this->value;
}
public function getValue() {
return (float)$this->value;
}
public function printme() {
echo (float)$this->value;
}
}
function printNumber(printable $myObject) {
$myObject->printme();
}
$inst = new Integer(10);
printNumber($inst);
echo PHP_EOL ;
$inst = new Floats(100.234);
printNumber($inst);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment