php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #79574 ReflectionType and gettype return different values
Submitted: 2020-05-08 13:07 UTC Modified: 2020-06-28 09:50 UTC
From: thomas dot wleight at googlemail dot com Assigned:
Status: Suspended Package: Variables related
PHP Version: 7.3.17 OS:
Private report: No CVE-ID: None
 [2020-05-08 13:07 UTC] thomas dot wleight at googlemail dot com
Description:
------------
There is a difference in results.
ReflectionParameter getType() function returns "int".
But builtin function "gettype" returns "integer".
Just check the test script below.

Test script:
---------------
class A {
    function f(int $a) {}
}
$r = new ReflectionClass("A");
$m = $r->getMethods();
$p = $m[0]->getParameters();
$t = $p[0]->getType();

echo $t; // "int"

echo PHP_EOL;

echo gettype(42); // "integer"

Expected result:
----------------
In both cases should return "integer"

Actual result:
--------------
ReflectionParameter returns "int" and gettype() returns "integer"

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-05-08 13:09 UTC] thomas dot wleight at googlemail dot com
This as an inconsistency issue. Please stick to one only variant: "int" XOR "integer". Using both variants is ambiguous.
 [2020-05-08 13:39 UTC] cmb@php.net
-Status: Open +Status: Suspended -Package: *General Issues +Package: Variables related -Assigned To: +Assigned To: cmb
 [2020-05-08 13:39 UTC] cmb@php.net
The values returned by gettype() ar#e unlikely to be ever changed
for backward compatibility reasons.  In PHP 8 the new function
get_debug_type()[1] will be available, which returns "int".

If you strongly feel that the return values of gettype() should be
changed, feel free to pursue the RFC process[2].  For the time
being, I'm suspending this ticket.

[1] <https://wiki.php.net/rfc/get_debug_type>
[2] <https://wiki.php.net/rfc/howto>
 [2020-05-08 13:52 UTC] thomas dot wleight at googlemail dot com
Maybe it would be easier just to change the reported type in "ReflectionParameter" instead of changing it the function "gettype()"?
 [2020-05-08 14:02 UTC] thomas dot wleight at googlemail dot com
oh, i see function/method typehint can be literally anything. Something like this would be hard to check and compare - "getType()" (ReflectionParameter) results with "gettype()" builtin function:

function foo(int $i, string $s, Foo $foo, Bar\Baz $barbaz) {
  ...
}

int
string
Foo
Bar\Baz
 [2020-05-08 14:28 UTC] cmb@php.net
> Maybe it would be easier just to change the reported type in
> "ReflectionParameter" instead of changing it the function
> "gettype()"?

Besides that this would be a BC break the other way round, "int"
is the canonical type name, and "integer" is an alias for BC
reasons.
 [2020-06-28 09:50 UTC] cmb@php.net
-Assigned To: cmb +Assigned To:
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 14:01:32 2024 UTC