note 129489 added to function.gettype
| From: | brainsmith.maleficent+php@gmail.com | Date: | Wed, 15 May 2024 08:33:48 +0000 |
| Subject: | note 129489 added to function.gettype | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221325@lists.php.net to get a copy of this message | ||
Since ReflectionParameter::getType() and gettype() do not return the same type name, you can
leverage the strpos() function.
use DateTime;
use ReflectionClass;
$myVar = 45;
# get the type of your variable
$varType = gettype($myVar);
$cls = new ReflectionClass(DateTime::class);
$mth = $cls->getMethod('setDate');
$params = $mth->getParameters();
foreach($params as $param) {
  $paramTypes = $param instanceof ReflectionUnionType
  ? $param->getTypes()
  : [$param];
  foreach($paramTypes as $paramType) {
    if (strpos($paramType, $varType) >= 0) {
      echo "Parameter '". $paramType->getName()
."' has type '". $paramType->getType() ."' which matches the
variable's type of '$varType'.\n";
    }
  }
}
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.13.131 (proxied: 109.165.232.194)
----
Manual Page -- https://php.net/manual/en/function.gettype.php
Edit -- https://main.php.net/note/edit/129489
Del: integrated -- https://main.php.net/note/delete/129489/integrated
Del: useless -- https://main.php.net/note/delete/129489/useless
Del: bad code -- https://main.php.net/note/delete/129489/bad+code
Del: spam -- https://main.php.net/note/delete/129489/spam
Del: non-english -- https://main.php.net/note/delete/129489/non-english
Del: in docs -- https://main.php.net/note/delete/129489/in+docs
Del: other reasons-- https://main.php.net/note/delete/129489
Reject -- https://main.php.net/note/reject/129489
Search -- https://main.php.net/manage/user-notes.php