Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98429 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 28801 invoked from network); 8 Mar 2017 15:15:25 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 8 Mar 2017 15:15:25 -0000 Authentication-Results: pb1.pair.com header.from=derokorian@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=derokorian@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.218.48 as permitted sender) X-PHP-List-Original-Sender: derokorian@gmail.com X-Host-Fingerprint: 209.85.218.48 mail-oi0-f48.google.com Received: from [209.85.218.48] ([209.85.218.48:35777] helo=mail-oi0-f48.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 1A/F6-06022-D0020C85 for ; Wed, 08 Mar 2017 10:15:25 -0500 Received: by mail-oi0-f48.google.com with SMTP id 62so20231377oih.2 for ; Wed, 08 Mar 2017 07:15:25 -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=PpeaU6gWr9pNSpi0wdTEbNQfbjFFjnstSNl0P2TmLbE=; b=dkqg19N1CEPQ78j59l1oZ9uJeqETu/ty7uc6tsE4x8r5NYvmW3420jgrpza0MA4Z7N 38sk97VtNwt3HEGAkOResYiCOSN2Q9Ovr0Q40SM1Whl/QrIICnlOq7VLR6+0BsrJqQ3s OibeggEYjqJcTe/U+RPwNr4BOfNP70mBVI2oa2rAlulOkgABQ15OLcLiTQSZS9Af9pyA wM5S+HaVux3rh7xnBhXMfY1TPoPSClK8QJG+/UG/+Kg7n6rdvr0dlRx2rzn6krEBfVpD imO7tIE1kbj4CveUe7snvkYhFVXGg8dey2q3k3g/sxzBpAhoTS0n6xqqaFw1yQF8/aio og5g== 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=PpeaU6gWr9pNSpi0wdTEbNQfbjFFjnstSNl0P2TmLbE=; b=Jjim6msLoEOcG5kn+k8QZHmcMiLtqs+/IgAuJjZ3cIxX3NUQJPrzI07UWjHh7Sbevf qaCM3Pem10G373Ch0UMdqAx876dk3gxad5h7wMPIDQsbiXkw9zYjX8Azbjs3+/qH9056 MnWdub8f0OYe3o+EQNXdunvsoCNP0Mbs9EZcMJ4YoKE8y2eOgrD/ygYDBKAk+/iEZV+J ASwGX6qjRGDenOAxPccQQcvUHWjBTPd/RLIujBJZArt6y/KCG+ikV5wyVZaTw0cUIzBp SlwK3BZkRdP1yucqiz4oWLOll68tGfOB2Td+Ia5NRAxMnE9YPZ+MQPmJWxpUs/0HwxJL LEIw== X-Gm-Message-State: AMke39mw8zZET6lZeed/W8yuAGXFc3nvF8rVbdjJH0Uml4Q38a8Wp1OpbcRZiFkQb/u+EI7emCQpERhD/MJlqw== X-Received: by 10.202.252.23 with SMTP id a23mr3726887oii.153.1488986122462; Wed, 08 Mar 2017 07:15:22 -0800 (PST) MIME-Version: 1.0 Received: by 10.157.11.227 with HTTP; Wed, 8 Mar 2017 07:15:22 -0800 (PST) In-Reply-To: References: Date: Wed, 8 Mar 2017 08:15:22 -0700 Message-ID: To: Andrey Andreev Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary=001a113adbe6e7aba3054a39982c Subject: Re: [PHP-DEV] [Discussion] is_string(), string type and objects implementing __toString() From: derokorian@gmail.com (Ryan Pallas) --001a113adbe6e7aba3054a39982c Content-Type: text/plain; charset=UTF-8 Sorry, accidently sent in the middle of typing that... On Wed, Mar 8, 2017 at 7:42 AM, Ryan Pallas wrote: > > > 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) { return $bar.'foo'; } class Foo { private $val; public function __construct(string $val) { $this->val = $val; } public function __toString() { return $this->$val; } } echo foo(new Foo('this is ')); // this is foo But what happens if I change the foo function like: function foo(string &$bar) { $bar .= 'foo'; } $foo = new Foo('object'); foo($foo); var_dump($foo); // will this be an instance of Foo, or the string "objectfoo"?? If $foo remains an object in this scope, then the function is not modifying its value. If it becomes a string, it's an unexpected change IMO. It is probably fine in this case, but not in the case of a more complex object. > > > >> >> * 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 >> >> > --001a113adbe6e7aba3054a39982c--