Skip to content

Instantly share code, notes, and snippets.

@kelunik
Created December 22, 2016 10:53
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 kelunik/51d3e8620e0b54ea2c158d94689f423a to your computer and use it in GitHub Desktop.
Save kelunik/51d3e8620e0b54ea2c158d94689f423a to your computer and use it in GitHub Desktop.
Strict types sadness demo.
<?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);
}
}
<?php
class DriverA implements Driver {
private $callback;
public function set(callable $callback) {
$this->callback = $callback;
}
public function invoke($foo) {
($this->callback)($foo);
}
}
<?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);
}
}
<?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