Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:73319 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 51095 invoked from network); 20 Mar 2014 02:48:56 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Mar 2014 02:48:56 -0000 Authentication-Results: pb1.pair.com header.from=tjerk.meesters@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=tjerk.meesters@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.220.174 as permitted sender) X-PHP-List-Original-Sender: tjerk.meesters@gmail.com X-Host-Fingerprint: 209.85.220.174 mail-vc0-f174.google.com Received: from [209.85.220.174] ([209.85.220.174:47199] helo=mail-vc0-f174.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 4E/93-33112-5175A235 for ; Wed, 19 Mar 2014 21:48:55 -0500 Received: by mail-vc0-f174.google.com with SMTP id ld13so242521vcb.5 for ; Wed, 19 Mar 2014 19:48:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=Aff674CQ4yoIYl2AhAQXXrlQXeRL3veSg9y6Qk8Mx5U=; b=yJhBS39w7XJNUqkLGfI1KkpO+2XGntHTrmLiHuGfRGtps5prcvhi2CNnl3bg0v+e8r DCL0Ulq7maPkKRq49KT2UNyVtNOHNKs5/jIVFqeAOp9ORAhCsC5WZnZHy2Xx4LCgDthb YjA+Q8TnmKpybe0UEwqPHQjemJbERnCcswZFB0/ehBPyJjuveEIa422kx7qwtPl8tPmj 7C6LTa6zCQvn45e40hCJDmK0B2eV+KArzjKbS8w6VzbjhoR6sVy3XR7oFJkmceZ9FWD9 F6UP+61oLZ7TMFX4FHXcXbJv9e9csGl3WJ+7R5h2ygN5u+BTDacJuCghZ/kED0az0AKk lA2w== MIME-Version: 1.0 X-Received: by 10.221.20.199 with SMTP id qp7mr14436467vcb.24.1395283730772; Wed, 19 Mar 2014 19:48:50 -0700 (PDT) Received: by 10.58.55.131 with HTTP; Wed, 19 Mar 2014 19:48:50 -0700 (PDT) In-Reply-To: References: Date: Thu, 20 Mar 2014 10:48:50 +0800 Message-ID: To: Chris Wright Cc: PHP Internals Content-Type: multipart/alternative; boundary=001a11339e2e223a6004f500cf4a Subject: Re: [PHP-DEV] Re: Array dereference on scalar / null values From: tjerk.meesters@gmail.com (Tjerk Meesters) --001a11339e2e223a6004f500cf4a Content-Type: text/plain; charset=ISO-8859-1 Hi Chris, On Wed, Mar 19, 2014 at 5:21 PM, Chris Wright wrote: > > On 18 Mar 2014 06:17, "Tjerk Meesters" wrote: > > > > Hi internals, > > > > I've done some bug report gathering on essentially the following > behaviour: > > > > $a = 123; var_dump($a[1]); // "NULL" > > $b = true; var_dump($b[1]); // "NULL" > > $f = fopen('file', 'r'); var_dump($f[1]); // "NULL" > > $n = null; var_dump($n[1]); // "NULL" > > > > Bug reports: > > > > https://bugs.php.net/bug.php?id=37676 > > https://bugs.php.net/bug.php?id=40692 > > https://bugs.php.net/bug.php?id=62769 > > https://bugs.php.net/bug.php?id=64194 > > https://bugs.php.net/bug.php?id=65484 > > > > From the comments it seems that there's some disagreement within the > group, > > for example: > > > > Nikita: "I would tentatively classify this as a bug." > > Tony: "Reclassified as feature request.." > > Xinchen: "there was a similar bug report of this behavior, and I seems > made > > a fix for it." > > Ilia: "this is not a bug." x 2 > > Joe: "it is the only reasonable thing _to expect_" > > > > From my understanding, developers are expecting to see notices when this > > happens, something like "Cannot use integer/boolean/resource/null as > array > > in %s on line %d"; I think they're okay with the fact that the end result > > will still be "NULL", though. > > > > The `list()` operator has since complicated matters; if we would indeed > > introduce notices as requested, the following code would cause issues: > > > > $array = [1, 2, 3]; > > while (list($key, $value) = each($array)) { > > echo "$key = $value\n"; > > } > > > > The return value of `each()` - as it has reached the end of an array - is > > `false`, so the above code would show two notices. Unfortunately, we must > > assume that all code in the manual are actually used and so it would > surely > > break stuff when introduced. > > > > We could mitigate this issue by having `each()` return `null` instead and > > introduce notices for all other scalars, but this code is not unlikely: > > > > while (($data = each($array)) !== false) { > > echo "${data[0]} = ${data[1]}\n"; > > } > > > > So my question is, how should we handle this? Close all bug reports and > add > > something to the documentation? Allow array dereference only on booleans > > *and* `null`? > > > > The alternative is treating list() as the special case and suppressing the > error there. I'd prefer this over allowing bool/null to be dereferenced > silently in the general case, since it would keep the special handling > where it belongs - I.e. where the conflict is. > The problem is that `list()` is not an opcode per se, but rather a code generator; it generates assignment opcodes for each specified variable, so: list($key, $value) = each($array); Gets compiled into: $tmp = each($array); $key = $tmp[0]; $value = $tmp[1]; From what I gather, what you have proposed only has a chance of working if the compiler could be told to perform the assignment and silently discard any issues that arise from doing an array dereference on something that's not an array (or object that looks like an array). Perhaps that's possible, I'm not sure :) > > > Best, > > Jack > -- -- Tjerk --001a11339e2e223a6004f500cf4a--