Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98430 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 30482 invoked from network); 8 Mar 2017 15:26:36 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 8 Mar 2017 15:26:36 -0000 Authentication-Results: pb1.pair.com smtp.mail=narf@devilix.net; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=narf@devilix.net; sender-id=pass Received-SPF: pass (pb1.pair.com: domain devilix.net designates 74.125.82.173 as permitted sender) X-PHP-List-Original-Sender: narf@devilix.net X-Host-Fingerprint: 74.125.82.173 mail-ot0-f173.google.com Received: from [74.125.82.173] ([74.125.82.173:33153] helo=mail-ot0-f173.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id FE/47-06022-BA220C85 for ; Wed, 08 Mar 2017 10:26:35 -0500 Received: by mail-ot0-f173.google.com with SMTP id 19so33190244oti.0 for ; Wed, 08 Mar 2017 07:26:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=devilix.net; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=yxEIb2ubAxM1wKp4xoFOO75Qx+EcT9oGJ14FRkwAYGc=; b=niEJZ7/NRdXZpp3BEjd2cJR1Y/npP1uKph4g/nrUqTc5gX0/sCw9iMXs2d5J3f3cpe nmq8WnZE/tDVekk9aU+InQBSsF85V+4txHjSwJc9s1qugvxUafSYg+PFFNJ1CrY4s1Fr wSnxm/WOs2XbWkk+Z0bku1btAVEuygxTR+HTo= 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=yxEIb2ubAxM1wKp4xoFOO75Qx+EcT9oGJ14FRkwAYGc=; b=Sm9YqupzYAf30q6ZqpJcXARMcqx7QbP0XTOsLH4wqX/oHig1yOF11uiDi45qeSk5Ww vlCDvHDaZmRveKceYInEBN6v4+xZjIHm5lPvQoDDnYF1G2NJu0aywHATGbA3e3h6rvrg A56awgzF3ZuWYwOGPcYnP8yExW56lplKuQxqypRvMDSoaCqUaEDwpAnZ4qo3aKZfxOum evxx2X98nwX3sDfdEjKqLOtOJgpU6Uj1iXhhJbHXxCr425YoisFiYq9evcT5fxdahlis ujGYl8xsacpA2auSQFx6W0vj8bhwjtWhPJFKx84pAutSSp5I+JYUhz1NfU88K8MOJl2k xG2A== X-Gm-Message-State: AFeK/H0y1jUDappq90T0TO9di+5mnC5qPe8/1EQmHxhgJgx9TiT8zMrNUGV+hA8Qa9BInRgmjMslI0t7n1bRfA== X-Received: by 10.157.17.147 with SMTP id v19mr3553552otf.117.1488986792664; Wed, 08 Mar 2017 07:26:32 -0800 (PST) MIME-Version: 1.0 Received: by 10.182.242.83 with HTTP; Wed, 8 Mar 2017 07:26:32 -0800 (PST) In-Reply-To: References: Date: Wed, 8 Mar 2017 17:26:32 +0200 Message-ID: To: Ryan Pallas Cc: "internals@lists.php.net" Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] [Discussion] is_string(), string type and objects implementing __toString() From: narf@devilix.net (Andrey Andreev) Hi Ryan, On Wed, Mar 8, 2017 at 5:15 PM, Ryan Pallas wrote: > 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. This already works and while the reference thing is indeed ugly, the problems I have with it are different: 1) It also accepts every other scalar type. 2) It will not work with strict_types=1. 3) I want to do it mid-runtime, not just on function parameters. Cheers, Andrey.