Req #79961 [Wfx]: Reflection Type method "GetName" returns different values then old gettype()

From: Date: Wed, 12 Aug 2020 10:07:43 +0000
Subject: Req #79961 [Wfx]: Reflection Type method "GetName" returns different values then old gettype()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-228539@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79961&edit=1 ID: 79961 Updated by: nikic@php.net Reported by: petr dot vejchoda at seznam dot cz Summary: Reflection Type method "GetName" returns different values then old gettype() Status: Wont fix Type: Feature/Change Request Package: Reflection related Operating System: Win10 PHP Version: 7.4.9 Block user comment: N Private report: N New Comment: See also https://wiki.php.net/rfc/get_debug_type for details on the referenced function. Previous Comments: ------------------------------------------------------------------------ [2020-08-12 10:05:40] 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:01:02] 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. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=79961&edit=1

« previous php.bugs (#228539) next »