Created
December 22, 2016 10:53
-
-
Save kelunik/51d3e8620e0b54ea2c158d94689f423a to your computer and use it in GitHub Desktop.
Strict types sadness demo.
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 | |
interface Driver { | |
public function set(callable $callback); | |
public function invoke($foo); | |
} | |
<?php | |
class DriverA implements Driver { | |
private $callback; | |
public function set(callable $callback) { | |
$this->callback = $callback; | |
} | |
public function invoke($foo) { | |
($this->callback)($foo); | |
} | |
} |
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 | |
class DriverA implements Driver { | |
private $callback; | |
public function set(callable $callback) { | |
$this->callback = $callback; | |
} | |
public function invoke($foo) { | |
($this->callback)($foo); | |
} | |
} |
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 | |
declare(strict_types=1); | |
class DriverB implements Driver { | |
private $callback; | |
public function set(callable $callback) { | |
$this->callback = $callback; | |
} | |
public function invoke($foo) { | |
($this->callback)($foo); | |
} | |
} |
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 | |
require __DIR__ . "/Driver.php"; | |
require __DIR__ . "/DriverA.php"; | |
require __DIR__ . "/DriverB.php"; | |
$driverA = new DriverA; | |
$driverB = new DriverB; | |
$callback = function (string $foo) { | |
}; | |
$driverA->set($callback); | |
$driverB->set($callback); | |
$driverA->invoke(42); | |
$driverB->invoke(42); | |
// Results in | |
// PHP Fatal error: Uncaught TypeError: Argument 1 passed to {closure}() must be of the type string, integer given, called in /tmp/DriverB.php on line 13 and defined in /tmp/test.php:10 | |
// Stack trace: | |
// #0 /tmp/DriverB.php(13): {closure}(42) | |
// #1 /tmp/test.php(18): DriverB->invoke(42) | |
// #2 {main} | |
// thrown in /tmp/test.php on line 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment