Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101278 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 2856 invoked from network); 9 Dec 2017 10:21:56 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 9 Dec 2017 10:21:56 -0000 Authentication-Results: pb1.pair.com header.from=ocramius@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ocramius@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.48 as permitted sender) X-PHP-List-Original-Sender: ocramius@gmail.com X-Host-Fingerprint: 74.125.82.48 mail-wm0-f48.google.com Received: from [74.125.82.48] ([74.125.82.48:38896] helo=mail-wm0-f48.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C8/44-62356-249BB2A5 for ; Sat, 09 Dec 2017 05:21:55 -0500 Received: by mail-wm0-f48.google.com with SMTP id 64so6642524wme.3 for ; Sat, 09 Dec 2017 02:21:54 -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=7TTKyT7SekaKxFK8hGFErKekyMXDTdKacVgqcpIFww8=; b=Bh5cNRIsYDLliq94SYDxm2VGvvAmFnh4kPAekqbGbR/ZYXo/a8hik/tkEgqwiK/Yv/ 9nGkafPgzIcuN60PlrWEfD7nEJDVuryswByxcuO9XzLlKSjBV2AWmvJ1Yhx68C1Mx66Z tqyNaSGkrtb3LJA182EZaunkGR0pyvoVnLfQfra1R5II3pHMa1A5ze31+MIEx972u3Vb 66ingt1deG0BmifAOcK3kIteZWweeP6PFTaT4rG4N0Awf4/IQ8dgeIs3cPcMf0R0aVgQ dQofV+1RidkIbXWGlev8Kw70ZYK4gvNlA1LzFGuhi8/ULV1ePsPaFKv7oKr8tSb3o/ah zojQ== 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=7TTKyT7SekaKxFK8hGFErKekyMXDTdKacVgqcpIFww8=; b=oUCsM5090MvaOEj6ifDV1XKPRoidVzOltTYX2CsJ0yBfyQiRquBH62PipivQg5mfGq CrkylzCzjQ29JHwuGgRpEcUyKIk5z6dihjYIbuvhhxjTO4ivFAu+et+mpbYNE870Xfdf f72/vfkOVPfmJFy98J8uizpel7rMa8s0HoSo7jcOCj53lbnkVwtZJ4mDa7zXXqYlAnuL LQ99oVh7OYprslcpw0XBSkNhXMVc/t+lVWGIeNyhMfPogUgOKoZIBetgzy3wCZfncuAH 6SV+XK4MeHIJhFkrQW4ZxtwW7h9brRVfM6zmSSYnYnsp39JWCVG8HiH3uFJk2q37W+N+ KUuQ== X-Gm-Message-State: AKGB3mL8oJoX46mwxteBQ5pd0/tnvjBNe6yhkpikfvV989YCzohlHbrV 54DP/aCOX+fYde4GWkV2oy7IGmg0Tl3UCD9GiTc= X-Google-Smtp-Source: AGs4zMac67lEq9QsBlx+4iCKEDilbBBPScE9x1YxYXs46tJHwFUY6HRAipnmFjBFbuMBrBwEq3A+ExNIS+nYv4HCdrk= X-Received: by 10.28.168.133 with SMTP id r127mr5718212wme.83.1512814911818; Sat, 09 Dec 2017 02:21:51 -0800 (PST) MIME-Version: 1.0 Received: by 10.223.184.205 with HTTP; Sat, 9 Dec 2017 02:21:49 -0800 (PST) Received: by 10.223.184.205 with HTTP; Sat, 9 Dec 2017 02:21:49 -0800 (PST) In-Reply-To: References: Date: Sat, 9 Dec 2017 11:21:49 +0100 Message-ID: To: Andreas Hennings Cc: Kalle Sommer Nielsen , PHP Internals List Content-Type: multipart/alternative; boundary="001a114cc6c46e1616055fe5ab8a" Subject: Re: [PHP-DEV] instanceof survives non-object variables, but crashes on non-object constants. From: ocramius@gmail.com (Marco Pivetta) --001a114cc6c46e1616055fe5ab8a Content-Type: text/plain; charset="UTF-8" The typical usage of `instanceof` is checking a `null|object` API that built an object of a (presumably known) type. Adding a crash there seems silly and overcomplicated. On 9 Dec 2017 07:57, "Andreas Hennings" wrote: > Why is the usage broken? It works :) > In practice, the current version of instanceof has an is_object() built-in. > This is what people could assume from trying. > > Of course I don't know how it is implemented internally. Maybe a kitten > dies every time I use instanceof on non-object variables? > > If we need an additional is_object() it can make some code more expensive.. > > > > > On 9 December 2017 at 07:46, Kalle Sommer Nielsen wrote: > > > Hi > > > > 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. > > > > If your code is like the example, it should be updated and warned about > as > > you are doing something illogical by first checking the type and or value > > of the operand you are passing to instanceof after. > > > > The impact should be minimal as is, so persevering bc for broken usage is > > a poor argument imo > > > > > > > > On 9 Dec 2017 07.38, "Andreas Hennings" wrote: > > > >> Adding a warning to something that used to be ok would break existing > >> code. > >> I remember a number of cases where I used instanceof on possibly > >> non-objects. > >> > >> if ($x instanceof C) { > >> return $x; > >> } > >> elseif ($x === NULL) { > >> ... > >> } > >> > >> All such code would then produce warnings. > >> > >> > >> On 9 December 2017 at 07:35, Kalle Sommer Nielsen > wrote: > >> > >>> Hi > >>> > >>> We should just add a warning to the first example, it seems like an > >>> oversight that it was left silent > >>> > >>> On 9 Dec 2017 07.29 <20%2017%2007%2029>, "Andreas Hennings" < > >>> andreas@dqxtech.net> 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. > >>>> > >>> > >> > --001a114cc6c46e1616055fe5ab8a--