Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101282 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 18341 invoked from network); 9 Dec 2017 15:19:40 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 9 Dec 2017 15:19:40 -0000 Authentication-Results: pb1.pair.com smtp.mail=aidantwoods@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=aidantwoods@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.53 as permitted sender) X-PHP-List-Original-Sender: aidantwoods@gmail.com X-Host-Fingerprint: 209.85.215.53 mail-lf0-f53.google.com Received: from [209.85.215.53] ([209.85.215.53:37541] helo=mail-lf0-f53.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B4/56-62356-90FFB2A5 for ; Sat, 09 Dec 2017 10:19:38 -0500 Received: by mail-lf0-f53.google.com with SMTP id a12so14746870lfe.4 for ; Sat, 09 Dec 2017 07:19:37 -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=oUor3aDKdDOIb+6DUKIv49/TdL52WD3E0CSo6HFCG6g=; b=CL3Z4ic0PBy7Q7ic5ag6LiKe9sgSTa5wRgmsS+JD/MyuLkr1pcDxwj+rpbknQR1TEL i2fKxqdfdmQceCYkZDj1GXLZ2EoTg1XQvlCB7+5OJq8/jFlRCQPLUTSmfuhGmCLylKi6 7BOhyuyLot3jFWVPdfWdZG7e9+bt2QAhhAvYuXmdLSL1HtFHLglDSgKs4M5Q9SNBYY6y Vib6P3dY49KFzhwu1hlX3pTH1rxrpXK0tuw9Oc2uJ/BiRjTffU+DYkvefzRJNuktKVXe 8DT89LHwYmX7TJMwZBUitzGscSh8ml1mNdDLC/vjfmcQhE7VwcO/25fhVFnGab5lUl0i O6gw== 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=oUor3aDKdDOIb+6DUKIv49/TdL52WD3E0CSo6HFCG6g=; b=Au8T7MwS2VLCdRUG60reH6EjxrykBg84PelG4kNkkAs5lp3Y531zb/3A21XCComsYV pZuabyxGNb4T7amIi/JPKpnQg3C8ejRG63lTKlXeXOtnUDhrdWGfa7xkAgTP/W0cq3DX PW6/QQeutpxPIXDgdDL/EgAexaC3o2rNozWLmBCKwxe+tYqUeINgU2bQKs5cXFrqJqAk ZAsX4efHq7tq6gAW/IzGMNtNLt3lonhp3rU9y1V4Yfwn6JYluAlBi8cTHbyL2LI/C1ut NS2Qnoq87Ww8B/z3jRu58N1DORZB+V7BotAg0gy73k9Jeo4ZbRHjnqADNSg8x1AggBKw rYWg== X-Gm-Message-State: AJaThX4OMjC8osTiHmjD93IvbwsA4k+2L7Ft19RjHeFdYsCzOvoAJJ24 F7gNoj9dvBFvmKXegaJ8ThvrcxMS/mwXkcvfS3c= X-Google-Smtp-Source: AGs4zMZ2Tq/AKshS7c6pLwPdDx6O19FZ+46SfNN+ueOHnkP1i79vvnEqcAnxmxDsplIxaGhWot/GorjlWv1jQZeRh2E= X-Received: by 10.46.17.153 with SMTP id 25mr18500347ljr.36.1512832774709; Sat, 09 Dec 2017 07:19:34 -0800 (PST) MIME-Version: 1.0 Received: by 10.46.85.210 with HTTP; Sat, 9 Dec 2017 07:19:34 -0800 (PST) In-Reply-To: References: Date: Sat, 9 Dec 2017 15:19:34 +0000 Message-ID: To: Andreas Hennings , kalle@php.net, Aidan Woods Cc: PHP internals Content-Type: multipart/alternative; boundary="94eb2c1a615e2428c4055fe9d4a1" Subject: Re: [PHP-DEV] instanceof survives non-object variables, but crashes on non-object constants. From: aidantwoods@gmail.com (Aidan Woods) --94eb2c1a615e2428c4055fe9d4a1 Content-Type: text/plain; charset="UTF-8" > where the three lines return `false`, the third [...] Oops, that should say "the first two lines return `true`, the third `false`" (the point here being that they return something, as opposed to the later three, which throw). On 9 December 2017 at 15:13, Aidan Woods wrote: > 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. >> > > --94eb2c1a615e2428c4055fe9d4a1--