Created
September 3, 2017 18:41
-
-
Save technoknol/11b898de852fc9fa7bae7935e95e7b54 to your computer and use it in GitHub Desktop.
Type hinting with Interfaces PHP - Example 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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