Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98426 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 22637 invoked from network); 8 Mar 2017 14:43:00 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 8 Mar 2017 14:43:00 -0000 Authentication-Results: pb1.pair.com smtp.mail=derokorian@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=derokorian@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.170 as permitted sender) X-PHP-List-Original-Sender: derokorian@gmail.com X-Host-Fingerprint: 74.125.82.170 mail-ot0-f170.google.com Received: from [74.125.82.170] ([74.125.82.170:34626] helo=mail-ot0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 31/C5-06022-27810C85 for ; Wed, 08 Mar 2017 09:42:58 -0500 Received: by mail-ot0-f170.google.com with SMTP id o24so31924838otb.1 for ; Wed, 08 Mar 2017 06:42:58 -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=zySpbsj7cfQK3cskzdORaXCZixQrC8x+PSVlow25m1M=; b=rExPC+2xPcNqn9nykz3FW/Bcxg88ZZfYaPoy/MkRKHRE5EXlZI3cLUHZYuYFC2uH6X i9jXf2mkyEhl2krNmR2oIV7vPE8VElBMuItkbvcxbvAH1Xs0wiT3ss1HiGY7dHUhLX6h H2oZc+d6MMBpn7HMiNXmav41YONM2XsxdaRXc07DHzYNTHMGzbLE+4nsFIQ0ttuMb/m+ e1/xxcVw9Fe76G3mSUoWVyPtMfk8H7mKRc7b/gvxMAemijQN/UCYaXKaDrPaHfrLrmq7 t3XZg1VbQyZNqPRYTE7VegTdsqXTvVgoy35MmYQ0cnbVJndn5gozR+rDmPD+NjXCKeHD EKGA== 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=zySpbsj7cfQK3cskzdORaXCZixQrC8x+PSVlow25m1M=; b=CDSK04zCNlo9UXdWIyX2WxUXM3av0iRYXrWzfBpbRq5JR79KMwNdCznSNDEVlsZbPp PDLOoqO0OpG2GvmWIlPvTMoJ4+6KbMjVZGx5wa/UKkct0sYK8Ibip71Ubtz6JsWUv4qw GHegDAjRZ9TPmpZpgv9gBmoCa6XsrpM5oGSRsSKTM48XqaheFHmanVjGh5wiWA2exnPk ZwmzyN6XiwK77KFBZkIgZlaKs4C8zs0lRfPX6eAXFagp5ddx7Ei4WyP68tBtnFDULn/J AcSK64ctSujcA6I1fhlnl87I/n5czLQuBA6lqVHcj00/sa3Nugg35HYLOQojyAQl8FRi Veng== X-Gm-Message-State: AFeK/H0pMooQM08MMU+ePzeWhuHzyJ1vdoBYshJkXzx0LeTqfBQFABzeS8dMk6Jyk1zK+tkxkyXCUS1Y8LIcdw== X-Received: by 10.157.6.164 with SMTP id 33mr3372853otx.43.1488984175597; Wed, 08 Mar 2017 06:42:55 -0800 (PST) MIME-Version: 1.0 Received: by 10.157.11.227 with HTTP; Wed, 8 Mar 2017 06:42:55 -0800 (PST) In-Reply-To: References: Date: Wed, 8 Mar 2017 07:42:55 -0700 Message-ID: To: Andrey Andreev Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary=94eb2c04f74edcde4c054a3924c1 Subject: Re: [PHP-DEV] [Discussion] is_string(), string type and objects implementing __toString() From: derokorian@gmail.com (Ryan Pallas) --94eb2c04f74edcde4c054a3924c1 Content-Type: text/plain; charset=UTF-8 On Wed, Mar 8, 2017 at 5:25 AM, Andrey Andreev wrote: > Hi all, > > I submitted a GitHub PR* to allow objects implementing __toString() to > *optionally* pass is_string() validation. More verbose wording of my > motivation can be seen in the PR description, but here are the main > points: > > - Simpler way to do checks like: is_string($var) || > method_exists($var, '__toString') > - Can be used for stricter string parameter validation in > strict_types=0 mode (otherwise any scalar type is accepted) - Can be used for looser string parameter validation in strict_types=1 > mode (__toString() objects aren't accepted there) > - Regardless of the last 2 points, it is intentionally not limited to > parameter types > If I understand correctly, you want the following to work: declare(strict_type = 0); function foo(string $bar) { > > * https://github.com/php/php-src/pull/2408 > > --- > > I didn't have time to write this email right after submitting the > patch, and in the meantime got some feedback from Fleshgrinder on > GitHub, which I'll quote and address here: > > > Thanks for your effort and initiative. > > > > However, I strongly believe that this is the wrong approach. Adding a > flag to a function directly results in the fact that the function violates > the single responsibility principle. What we actually need to make this > work is a "stringable" pseudo-type like the iterable type that was > introduced in PHP 7.1. This "stringable" pseudo-type is the union of the > scalar primitive string and any class that implements the __toString method. > > > > This has the advantage that we are actually able to use it together with > strict_types, plus we have separate dedicated functions like > "is_stringable" that adhere to the single responsibility principle. I > actually wanted to create an RFC for that along with an implementation > since iterable was accepted, but did not find the time yet. > > > > Closing note: these pseudo-types are necessary in PHP because it has no > coherent type system, and there is nothing we can do about this in short > term. Hence, adding such pseudo-types is the only short term solution that > we actually have. > > I ultimately wouldn't care if it's a separate function and did in fact > think of an is_stringable() function, but wasn't happy with the naming > - who's to say that e.g. integers aren't stringable? Bar > horribly-verbose names like > "string_or_objects_implementing__toString", I don't think there's a > way to avoid that ambiguity. :/ > If we want a "stringable" type though, I guess we'll have to live with > that. > > I feel that debating the actual type system is way broader than I > intended this to be, so I'll refrain from going further on that for > now, as I've got some more radical ideas about it. > > --- > > Thoughts? > > Cheers, > Andrey. > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --94eb2c04f74edcde4c054a3924c1--