Doc #72491 [Ver->Csd]: Case can take objects and arrays

From: Date: Sat, 25 Jun 2016 11:21:06 +0000
Subject: Doc #72491 [Ver->Csd]: Case can take objects and arrays
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-13601@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: Case can take objects and arrays -Status: Verified +Status: Closed Type: Documentation Problem Package: Scripting Engine problem Operating System: Mac PHP Version: 5.6.23 Assigned To: cmb Block user comment: N Private report: N New Comment: This bug has been fixed in the documentation's XML sources. Since the online and downloadable versions of the documentation need some time to get updated, we would like to ask you to be a bit patient. Thank you for the report, and for helping us make our documentation better. Previous Comments: ------------------------------------------------------------------------ [2016-06-25 11:20:20] cmb@php.net Automatic comment from SVN on behalf of cmb Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=339487 Log: Fix #72491: Case can take objects and arrays Indeed, there are no restrictions on the type that a case expression evaluates to. That's conforming to the PHP language specification: &lt;https://github.com/php/php-langspec/blob/PHP-5.6/spec/11-statements.md#the-switch-statement&gt; ------------------------------------------------------------------------ [2016-06-25 11:11:07] cmb@php.net Confirmed: <https://3v4l.org/6ZFjk>; arrays are also allowed: <https://3v4l.org/n2MhZ>. ------------------------------------------------------------------------ [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

« previous php.doc.bugs (#13601) next »