Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101281 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 16887 invoked from network); 9 Dec 2017 15:13:40 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 9 Dec 2017 15:13:40 -0000 Authentication-Results: pb1.pair.com header.from=aidantwoods@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=aidantwoods@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.47 as permitted sender) X-PHP-List-Original-Sender: aidantwoods@gmail.com X-Host-Fingerprint: 209.85.215.47 mail-lf0-f47.google.com Received: from [209.85.215.47] ([209.85.215.47:33924] helo=mail-lf0-f47.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F3/06-62356-3ADFB2A5 for ; Sat, 09 Dec 2017 10:13:39 -0500 Received: by mail-lf0-f47.google.com with SMTP id x20so14775313lff.1 for ; Sat, 09 Dec 2017 07:13:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=kX7MxHcJPIxmRlKTRrCxXE56aUXW/GJXyhZdkiCECAI=; b=qyOVvXklPW43+iF2CGXXjbRBLfLImaB6Q62+520x4h9lHdp+ncOqxvahjk+rocFU3W M89xMG6k4VIJJmOlLiy/+ZnN11CtWd3FbZS5divKSfcVaYOIDRqozrGE6ojqBCQA+D2G 1we7THChEDyqI+5nMBAgGtRLeVMh/retxZr4A/pOyQQpmQAE3540TRvMEzu+oQj6RGKJ 1A3mJI2dacW/xROqPnp63Zsx+Zhus9GZLy9oCZhTJvDvJ1CqHqmrUVVfPQ6G9EbLov9v NQTc+ZJDJRrnUTDWx/gTomLwnjs4XiRt0J/fs+7dYnChWjsR07dX6/KordWmEFEohTOi L2xg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=kX7MxHcJPIxmRlKTRrCxXE56aUXW/GJXyhZdkiCECAI=; b=BGHIiijsoE+u1oLJP74y7FQnj3rP3el6v1jwpEpQsqtdgs4Veros3NK5KjQggJg0Fs gyLIAuOhwwuc2PDYERemHFuO4ENCbSZioEs+xG7uoZVs0ePVB+iqnNDmd8MhqENht2M8 Trhf3JJdLG8WLF+KC4fuavktj4P5W4aOTF91FmhRm6sS7Rw7/g5m+m+rPiVOoJfKWqsR HXvgIea/3RRuRXsx/sD8xX5GZdbYEMtwp3UDZDC2rKNYhiFMLrDmBHOn+PUl0CtJHnTA imLONhxO+gY6Z3JmjXVAKBhdjp8bV6M7n87XE47c+ChpuynPOG+Gou40LjxlaNflD5C0 7ckw== X-Gm-Message-State: AJaThX64+O5uXHYrUE1b7dm+b4DEjJPJNL8vEtSMAgcwx4DbN103kOjD 3nwjScqVjT9fK4g1oADcw5LW76Q3kKPrt2PFnMX3/L4j X-Google-Smtp-Source: AGs4zMYPx1LmiDXCYoZsHFZf0ypvNCYEJD3b33k7uhowZYWWhn/8WyIhvRrCI9Hik9Hs9ddalGhzkGCrsBGlMJbiJfo= X-Received: by 10.46.82.89 with SMTP id g86mr18384589ljb.131.1512832416300; Sat, 09 Dec 2017 07:13:36 -0800 (PST) MIME-Version: 1.0 Received: by 10.46.85.210 with HTTP; Sat, 9 Dec 2017 07:13:35 -0800 (PST) In-Reply-To: References: Date: Sat, 9 Dec 2017 15:13:35 +0000 Message-ID: To: Andreas Hennings , kalle@php.net Cc: PHP internals Content-Type: multipart/alternative; boundary="001a113c2f4ec74367055fe9beee" Subject: Re: [PHP-DEV] instanceof survives non-object variables, but crashes on non-object constants. From: aidantwoods@gmail.com (Aidan Woods) --001a113c2f4ec74367055fe9beee Content-Type: text/plain; charset="UTF-8" I prefer the inconsistency between `$x instanceof \stdClass` and `1 instanceof \stdClass`, than adding a warning (or throwing) to `$x instanceof \stdClass` when `$x = 1`. I think `1 instanceof \stdClass === false` would be reasonable. 1. PHP has no means of locking a variable to a type (i.e. the variable could be reassigned to a scalar at any point), so lack of an implicit `is_object` check in `instanceof` would basically move to require an explicit one in many cases it is used, if you want to be *sure* a warning won't be thrown that is (baring in mind just using the variable earlier might change its type non-visibly, since we don't have explicit caller opt-in to pass-by-reference). This to say that IMO `$x instanceof stdClass` when `$x = 1` perhaps shouldn't be considered broken usage because it is useful for it to perform this check. Also consider that `is_object($x) === false` implies `$x` is not an instance of any object (and in-particular not of the object you might ask about with `instanceof`), so there is no reason `instanceof` wouldn't be able to do this check. There is also a certain amount of irony about making a type-checking operator start complaining when you give it the wrong type ;-) 2. There is already precedent for variables to act differently than literals, and even constants in PHP when used in object operators, for example ``` $x = new \stdClass; const y = 'stdClass'; $y = y; var_dump($x instanceof $x); var_dump($x instanceof $y); var_dump($x instanceof y); var_dump($x instanceof 'stdClass'); var_dump($x instanceof new \stdClass); var_dump($x instanceof (new \stdClass)); ``` where the three lines return `false`, the third as (the const isn't looked at, and no warning is thrown when checking against non-existent `y` class). The last three lines throw errors. Point here is that, "should variables be treated like their literal values?" is perhaps a bigger question, to which the answer at present seems to be "no" of object operators. I'm not opposed to moving towards constancy, but I think there being inconsistency isn't worth throwing in `$x instanceof \stdClass` when `$x = 1` (since inconsistency between variables and their values shouldn't be unexpected when using object operators). --- @Kalle > We should just add a warning to the first example, it seems like an oversight that it was left silent & > That is fine for code that is broken in the first place. Similarly we added a warning some years back about array to string conversions. As a data-point, usage with non-objects is documented behaviour http://php.net/manual/en/language.operators.type.php#example-115 so would this be spec change to say this is incorrect, as opposed to being a bugfix? Kind regards, Aidan On 9 December 2017 at 06:28, Andreas Hennings wrote: > The following (https://3v4l.org/A2Tp6) is ok, it simply returns false: > > $x = 1; > $x instanceof \stdClass; > > > The following (https://3v4l.org/IdSBu) gives a fatal error: > > 1 instanceof \stdclass; > > t think this behavior is inconsistent, and we should consider changing it. > > There are two options, but only one is BC. > > - Let 1 instanceof \stdClass return false, instead of crashing. -> seems BC > - Let $x instanceof \stdClass crash, if $x is not an object. -> BC break. > > So it seems the first would the option we should take. > This is also what hhvm does, according to https://3v4l.org/IdSBu. > --001a113c2f4ec74367055fe9beee--