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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: petr dot vejchoda at seznam dot cz
New email:
PHP Version: OS:

 

 [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

Pull Requests

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: Fri Dec 27 03:01:28 2024 UTC