php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #79961 Reflection Type method "GetName" returns different values then old gettype()
Submitted: 2020-08-12 10:01 UTC Modified: 2020-08-12 10:07 UTC
From: petr dot vejchoda at seznam dot cz Assigned:
Status: Wont fix Package: Reflection related
PHP Version: 7.4.9 OS: Win10
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
40 - 13 = ?
Subscribe to this entry?

 
 [2020-08-12 10:01 UTC] petr dot vejchoda at seznam dot cz
Description:
------------
---
From manual page: https://php.net/class.reflectionnamedtype
---
Legacy php method gettype() returns different type names for variables then Reflection class methods making it unnecessarily complex to check whether method parameter is the same type of reflected typed property.  

Test script:
---------------
class testClass
    public int $int;
    public float $float;
    public bool $bool;
    public string $string;

    public final function __construct()
    {
        

        $intType = (new \ReflectionClass($this))->getProperty("int");
        $floatType = (new \ReflectionClass($this))->getProperty("float");
        $boolType = (new \ReflectionClass($this))->getProperty("bool");
        $stringType = (new \ReflectionClass($this))->getProperty("string");

        $int = 0;
        $float = 0.0;
        $bool = true;
        $string = 'string';

        var_dump(($intType->getName()));
        var_dump(($floatType->getName()));
        var_dump(($boolType->getName()));
        var_dump(($stringType->getName()));

        var_dump(gettype($int));
        var_dump(gettype($float));
        var_dump(gettype($bool));
        var_dump(gettype($string));

        die();
    }
}

Expected result:
----------------
Expect all booleans to be named the same, all integers, all floating numbers to be  named the same. So that I can check it by string comparison even though I hate to check types by string comparison ...

Actual result:
--------------
Mess. Total unholy mess.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-08-12 10:05 UTC] nikic@php.net
-Status: Open +Status: Wont fix
 [2020-08-12 10:05 UTC] nikic@php.net
The legacy gettype() function will not be changed for backwards-compatibility reasons. PHP 8 introduces get_debug_type() that uses canonical type names, and also returns class names.

However, your premise of checking whether a value satisfies a type declaration using gettype() is unworkable in the first place once non-trivial types get involved (esp. union types).
 [2020-08-12 10:07 UTC] nikic@php.net
See also https://wiki.php.net/rfc/get_debug_type for details on the referenced function.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 19:01:33 2024 UTC