Doc #72491 [Opn->Ver]: Switch can take a class
| From: | cmb@php.net | Date: | Sat, 25 Jun 2016 11:11:10 +0000 |
| Subject: | Doc #72491 [Opn->Ver]: Switch can take a class | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-13600@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=72491&edit=1
ID: 72491
Updated by: cmb@php.net
Reported by: anfurny22 at gmail dot com
Summary: Switch can take a class
-Status: Open
+Status: Verified
Type: Documentation Problem
-Package: Documentation problem
+Package: Scripting Engine problem
Operating System: Mac
PHP Version: 5.6.23
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
Confirmed: <https://3v4l.org/6ZFjk>; arrays are also
allowed:
<https://3v4l.org/n2MhZ>.
Previous Comments:
------------------------------------------------------------------------
[2016-06-24 23:41:56] anfurny22 at gmail dot com
Description:
------------
From http://php.net/manual/en/control-structures.switch.php
"The case expression may be any expression that evaluates to a simple type, that is, integer or
floating-point numbers and strings. Arrays or objects cannot be used here unless they are
dereferenced to a simple type."
However, when I pass an object, it seems to work fine.
Test script:
---------------
class X { public $j; }
class Y { public $j; }
$x = new X();
switch ($x) {
case new Y():
return "Matched a Y";
break;
case new X():
echo "Matched an X";
break;
default:
echo "No match";
break;
//outputs "Matched an X"
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=72491&edit=1