Req #62089 [Opn->Csd]: instanceof operator type variable from function
| From: | cmb@php.net | Date: | Wed, 31 Mar 2021 16:38:46 +0000 |
| Subject: | Req #62089 [Opn->Csd]: instanceof operator type variable from function | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-233105@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=62089&edit=1
ID: 62089
Updated by: cmb@php.net
Reported by: php dot net at kingsquare dot nl
Summary: instanceof operator type variable from function
-Status: Open
+Status: Closed
Type: Feature/Change Request
Package: *General Issues
Operating System: n/a
PHP Version: Irrelevant
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
This is possible as of PHP 8.0.0; you need to enclose the right
hand side of the instanceof operator in parentheses, though:
<https://3v4l.org/oBvfJ>
Previous Comments:
------------------------------------------------------------------------
[2012-05-21 09:56:46] php dot net at kingsquare dot nl
Description:
------------
This might be a 'feature request', a bug or just a documentation issue..
We can use the instanceof operator using variables:
i.e.
$objectType = 'StdClass'
$this instanceof $objectType
works flawlessly!
But in order to prevent introducing a variable needed just because of the check
i would have guessed i could be able to do:
$this instanceof get_class($this)
(ofcourse the actual usage would be something else, this is just to illustrate
the usage of a function to retrieve some type of class name)
Test script:
---------------
class Foo {
function getClass() {
return __CLASS__;
}
}
$bar = new Foo();
var_dump($bar instanceof $bar->getClass());
exit;
Expected result:
----------------
true
Actual result:
--------------
Parse error: syntax error, unexpected '('
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=62089&edit=1