Quantcast
Channel: Serverphorums.com
Viewing all articles
Browse latest Browse all 23908

Re: [PHP-DEV] hasType() for internal function parameters?

$
0
0
On Sun, Jun 21, 2015 at 6:26 PM, Rasmus Lerdorf <rasmus@lerdorf.com> wrote:

> Is it deliberate that we are not providing the parameter types for
> internal functions via reflection? It seems inconsistent:
>
> <?php
> declare(strict_types=1);
> function ustrlen(string $str) { }
> $param_ustrlen = (new
> ReflectionFunction('ustrlen'))->getParameters()[0];
> $param_strlen = (new ReflectionFunction('strlen'))->getParameters()[0];
>
> echo "$param_ustrlen (".$param_ustrlen->hasType().")\n";
> echo "$param_strlen (".$param_strlen->hasType().")\n";
>
> try {
> ustrlen(1);
> } catch (TypeError $e) {
> echo $e->getMessage()."\n";
> }
>
> try {
> strlen(1);
> } catch (TypeError $e) {
> echo $e->getMessage()."\n";
> }
>
> The output is:
>
> Parameter #0 [ <required> string $str ] (1)
> Parameter #0 [ <required> $str ] ()
> Argument 1 passed to ustrlen() must be of the type string, integer
> given, called in /home/rasmus/prop.php on line 11
> strlen() expects parameter 1 to be string, integer given
>
> That is, in both cases a TypeError exception is raised because the type
> of the parameter is incorrect. But hasType() on the internal function
> parameter claims there is no type even though there obviously is one.
>
> -Rasmus
>

The difference here is that ustrlen() has an argument type specified in
arginfo, while strlen() triggers an error through zend_parse_parameters().
We have practically no arginfo-level type annotations in the standard
library.

Nikita

Viewing all articles
Browse latest Browse all 23908

Trending Articles