Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:86522 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 93958 invoked from network); 9 Jun 2015 13:09:35 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 9 Jun 2015 13:09:35 -0000 Authentication-Results: pb1.pair.com smtp.mail=cmbecker69@gmx.de; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=cmbecker69@gmx.de; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmx.de designates 212.227.15.15 as permitted sender) X-PHP-List-Original-Sender: cmbecker69@gmx.de X-Host-Fingerprint: 212.227.15.15 mout.gmx.net Received: from [212.227.15.15] ([212.227.15.15:62216] helo=mout.gmx.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B2/C4-00828-D85E6755 for ; Tue, 09 Jun 2015 09:09:34 -0400 Received: from [192.168.0.100] ([95.89.139.132]) by mail.gmx.com (mrgmx001) with ESMTPSA (Nemesis) id 0MF5FT-1Yr1ov0ygV-00GJv2; Tue, 09 Jun 2015 15:09:30 +0200 Message-ID: <5576E58B.2000404@gmx.de> Date: Tue, 09 Jun 2015 15:09:31 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: Rowan Collins , internals@lists.php.net References: <5576051A.3040800@gmx.de> <55760771.6020802@gmail.com> <778F345F9918474AB8CAAB03082ECBF7@pc1> <5576DC09.8050705@gmail.com> In-Reply-To: <5576DC09.8050705@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Provags-ID: V03:K0:/OOkv9tvJcYpIB2oW/fxI6LZe1Q/SZSZ1CfmObhcXh9EYPR+iET icV+PCjx1TaRN8QacoV2IWjNQQY9BL13Q8p+CYeDhL7Pa3W/jr02HzwSsU3lSLPeMom4UKQ m6UUbI2oJaD1NHCmtqnq7zInHc6Ez7mKCllZucRLpVj81sjMOFMJdd6FHEXeHl5Xw5icPnD rKl7p1BP9J8wmNbi0QsPA== X-UI-Out-Filterresults: notjunk:1;V01:K0:JXOrR6gVUn0=:gz9odw9rOShb9Vjdg9vH7K PAXiv/MaTNhiTNbZ3Gz+fIjqKt5I05baLrpZ9OGqjA4jIw4tgxKMY21bnuLoTQJ6J90mQI368 30RiEev1CzNeo/Zht4zqyy8VDwd7T6JFWyiWGaN0MSPY4ORMnxghTvpACDia09Nv9RlwgWofv lFnc5cIyfaDvi4oDqA38xQaug/Tfz0C5cyC6gpNkSCIGjtP9m6NLkDauaKVnoJt8C0C4Z7MjO 19Sz6XNabJ4M4OqmpYf/hCGMSaB0TbpskHCZNQHzT/H28kgl5/qDsxai0894ty5ZNKYskoI3w sW2/Fj9LKDGyHG5xXnv+ULjUj6W08W6LACZy8C7uEMTR4JhhJTvZIB2JjmxyvqEAigf7FSwFF AfiPtdH0Ct1D62mJh+MIFvOO/6nhRY7pzgggDihfPwBetxCtfn78ewoJuWoM5RXs6btOsd5bq v08H4E6Z9o35T2/8buxZuo9VS40g2hNQqaKvIF9tyRBVCfqe91gkELQcklozUR0SrX/RHHz+z fTC7PH8f/quKgN5+ZkXcmkHqZ41jj4mHuiRlOzehdC4079z9x4NfpiMY/TI6f+V1yl3LKd34/ 72UVyK8uaohk4gOBa3O+p0AmtSoDFHcOCmaRW9b7STZ6+Hbzja4lQduXvWaJRWwGnH4XXh0tH rUAioVPQ1E92r2/2E8oh0yR1BUU6A1DbOsnZ808Q9zzuFRwuKK7aSc6BkI+vGsM1keBE= Subject: Re: [PHP-DEV] Array dereferencing of scalars From: cmbecker69@gmx.de (Christoph Becker) Rowan Collins wrote: > Yasuo Ohgaki wrote on 09/06/2015 11:44: >> $v = NULL; >> $v[0][1][2][3][4][5][6][7][8][9]; // NULL >> >> this code is semantically wrong and I would like to have error/exception >> for such >> erroneous codes. > > PHP considers an uninitialised variable to have the value NULL, and a > NULL value to be coercable to any type, so this breaks down (logically, > not necessarily literally) as follows: > > - coerce $v to array() > - instantiate $v[0] as NULL > - coerce $v[0] to array() > - instantiate $v[0][1] as NULL > - and so on... > > Raising a notice whenever the type is coerced would be inconsistent with > other coercions (e.g. $foo = null; echo $foo + 1;). Raising a notice > whenever the coerced array is actually accessed would result in a notice > for every dimension, which would be very noisy. > > Ideally, it would give a single notice, as in the below, but I'm not > sure how that would be implemented: > > $v = array(); > var_dump($v[0][1][2][3][4][5][6][7][8][9]); > // Notice: Undefined index: 0 > // NULL > > Note that this is all rather different from the original case, which was > about values which *cannot be coerced to array*. I wonder where these coercion rules are described. The manual has a section about "Converting to array"[1] which actually describes casting, and is obviously not what is happening when the subscript operator (as the langspec calls it[2]) is applied to non-null scalars. The section "Accessing array elements with square bracket syntax"[3] doesn't describe the behavior of accessing elements of non-arrays at all. The language specification specifies as constraint: | If subscript-expression is used in a non-lvalue context, the element | being designated must exist. Finally, it is not even clear why a value should be coerced to array when the subscript operator is applied – it might be coerced to string as well. [1] [2] [3] -- Christoph M. Becker