<?php
function autoboxing_error_handler($errno, $errstr, $errfile, $errline, $errcontext) {
if (preg_match('/^Argument (\d+) passed to \S+ must be (?:an|an instance of|of the type) (\S+), \S+ given/', $errstr, $match)) {
$bt = debug_backtrace();
$value = $bt[1]["args"][$match[1] - 1];
if (strcasecmp($match[2], "array") == 0) {
$bt[1]["args"][$match[1] - 1] = array($value);
return true;
} else if (is_subclass_of($match[2], "Autoboxable")) {
$bt[1]["args"][$match[1] - 1] = new $match[2]($value);
return true;
}
}
return false;
}
set_error_handler("autoboxing_error_handler");
interface Autoboxable {
public function __construct($value);
}
class Integer implements Autoboxable {
public $value = null;
public function __construct($value) {
$this->value = (int)$value;
}
}
function foo(Integer $a, array $b) {
var_dump($a->value);
var_dump($b);
}
foo("1", 2);
preferences:
46.48 ms | 410 KiB | 5 Q