php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #72491 Case can take objects and arrays
Submitted: 2016-06-24 23:41 UTC Modified: 2016-06-25 11:21 UTC
From: anfurny22 at gmail dot com Assigned: cmb (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.6.23 OS: Mac
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: anfurny22 at gmail dot com
New email:
PHP Version: OS:

 

 [2016-06-24 23:41 UTC] 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"
        }


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-06-25 11:11 UTC] cmb@php.net
-Status: Open +Status: Verified -Package: Documentation problem +Package: Scripting Engine problem -Assigned To: +Assigned To: cmb
 [2016-06-25 11:11 UTC] cmb@php.net
Confirmed: <https://3v4l.org/6ZFjk>; arrays are also allowed:
<https://3v4l.org/n2MhZ>.
 [2016-06-25 11:14 UTC] cmb@php.net
-Summary: Switch can take a class +Summary: Case can take objects and arrays
 [2016-06-25 11:20 UTC] 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:21 UTC] cmb@php.net
-Status: Verified +Status: Closed
 [2016-06-25 11:21 UTC] cmb@php.net
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.
 [2020-02-07 06:07 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=762d0209c2940e8e53eaca0352a670f87621b82b
Log: Fix #72491: Case can take objects and arrays
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Thu Oct 08 03:00:02 2026 UTC