php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #62089 instanceof operator type variable from function
Submitted: 2012-05-21 09:56 UTC Modified: 2021-03-31 16:38 UTC
From: php dot net at kingsquare dot nl Assigned: cmb (profile)
Status: Closed Package: *General Issues
PHP Version: Irrelevant OS: n/a
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: php dot net at kingsquare dot nl
New email:
PHP Version: OS:

 

 [2012-05-21 09:56 UTC] 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 '('

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-03-31 16:38 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2021-03-31 16:38 UTC] cmb@php.net
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>
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 12:01:33 2025 UTC