Re: Strict typing and callback vs declare()
| From: | Dmitry Stogov | Date: | Thu, 26 Feb 2015 10:56:35 +0000 |
| Subject: | Re: Strict typing and callback vs declare() | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-83885@lists.php.net to get a copy of this message | ||
On Thu, Feb 26, 2015 at 1:34 PM, Benjamin Eberlei <kontakt@beberlei.de>
wrote:
>
>
> On Thu, Feb 26, 2015 at 11:10 AM, Dmitry Stogov <dmitry@zend.com> wrote:
>
>> Hi Anthony,
>>
>> What do you think about using a user level callback for strict type checks
>> instead of declare(). It won't allow changing behavior per file, but this
>> has its own cons and pros.
>>
>> <?php
>> set_strict_type_checker(function ($class_name, $function_nume, $arg_num,
>> $expected_type, $value, $file, $line) {
>> ...
>> return false;
>> });
>> include("orig_index.php");
>> ?>
>>
>> If callback is not set, arguments are converted according to standard
>> rules, if set and returns false - fatal error or exception is thrown.
>>
>> The implementation should be simpler and more efficient than using
>> declare().
>>
>> Thanks. Dmitry.
>>
>
> This ruins portability with third party libraries completely.
>
Not completely, because checker may be smart enough to return "true" for
third party files.
<?php
set_strict_type_checker(function ($class_name, $function_nume,
$arg_num,$expected_type, $value, $file, $line) {
if (!my_own_file($filename)) {
return true;
}
...
return false;
});
include("index.php");
?>
And you won't have to modify each file in your project adding
declare(strict_types=1).
Thanks. Dmitry.