Hi Rasmus, Although DbC is not what you need, but DbC could solve your issue more efficiently. i.e. Faster execution, not shorter code.
https://wiki.php.net/rfc/dbc2 With DbC, caller has responsibility to pass correct parameters. On Sun, Apr 9, 2017 at 6:30 PM, Rasmus Schultz <ras...@mindplay.dk> wrote: > > $one = "1"; > $one_int = (int) $one; > add_one($one_int); > add_one(&$value) require (is_int($value)) { $value += 1; } // Caller has responsibility to pass correct parameters. $one = filter_validate($_GET['var'], FILTER_VALIDATE_INT); add_one($one); > class Foo { public function __toString() { return "foo"; } } > function append_to(string &$str) { $str .= "_bar"; } > $foo = new Foo(); > append_to($foo); > var_dump($foo); // string(7) "foo_bar" class Foo { public function __toString() { return "foo"; } } function append_to(&$str) require (is_string($str)) { $str .= "_bar"; } $foo = new Foo(); // Caller has responsibility to pass correct parameters, but it's not append_to($foo); // Error at DbC precondition check in append_foo() var_dump($foo); // Cannot reach here in dev mode I really like parameter type check. Problem is type check makes execution slower. Another problem is type check is not enough for many codes. With DbC support, we can specify any expressions. Therefore, we can check much more complex requirements for functions/methods at development time. e.g. function save_age($user_age) require (is_int($user_age)) require ($user_age >= 0) require ($user_age < 150) { save_to_somewehre($user_age); } //Note: All input parameters must be validated to be correct value for the app. e.g. use filter_validate()/etc What you really need might be DbC. Regards, -- Yasuo Ohgaki yohg...@ohgaki.net