Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98463 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 48005 invoked from network); 9 Mar 2017 20:09:10 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 9 Mar 2017 20:09:10 -0000 Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.128.173 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 209.85.128.173 mail-wr0-f173.google.com Received: from [209.85.128.173] ([209.85.128.173:36208] helo=mail-wr0-f173.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C2/82-31244-566B1C85 for ; Thu, 09 Mar 2017 15:09:09 -0500 Received: by mail-wr0-f173.google.com with SMTP id u108so52313733wrb.3 for ; Thu, 09 Mar 2017 12:09:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:references:from:to:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding; bh=xJX9eO4hZk8tsdMIP4bPd8XLfBrrJ9lCMC1vP2rlrr4=; b=ZP9ZOKq77P05TvM612BQKxfPajK+cj4hEW/iZYlVaOlV2I5Nb8Y65Vj/mp6tHpxOAX 0bT5zSWUq5bT6zt5iwoJGu1Z4VgDGlcuC93PdZYEl7hC3gFyl1C+BtDb9gDr3vmM1sAL JfPN91aFTIuPidJLOSpPgk1kHp5I7mPjPZTx7/gt7mdndbdY/qhmlwUbzMivVcpbMP5U qufZhGCQqmtiDLeAvcXup4BLx9q5/sKmzr4SIkyNHZdUW9is1YBnYdYNROfNnlywHGnC rYVkIaA+MCP3sMmJCnpBHVSK6D9NJmC0Df7y+yPXsx570iSMOrAojS6bUay3izk3xQ6m MVcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:references:from:to:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=xJX9eO4hZk8tsdMIP4bPd8XLfBrrJ9lCMC1vP2rlrr4=; b=FoYWE/jupL2UExm40sAGMNCBhtXeToDM8bXc47ichzUTG1Nl8HQkG52s3jl5ORhQee kfJhJNYKvJlRl+Fw+lbc3VnKNxCzd7aFLTrUKZs8FwSTlbCUrgOemkeSl0cPYWZ3Zjma ErZu/SRzX10s6i8ZRCLY8+vKHT/E6qaqlvb99ZVewNpeFRsb6KyWOedFax67qef48DEl z7qCi7wXPxL2r4mxUwuWkI/pBkZMnkujwT3UaBiV0XywB/AzQ7hQAfmelJGUp8hQoCj5 Ld5pdX10AtHXvKj/RUTpYCP5ZviIhk146VPybHE21QOc5eEBjmvXm/ePjsBtpZvcFK6c BM6w== X-Gm-Message-State: AMke39nKpNP0NRReYf23mjTAJDv87dKcdXnn6s5SElPU3/wvL8rv+VNRMfbE0As/guZJkQ== X-Received: by 10.223.128.5 with SMTP id 5mr12051792wrk.163.1489090146008; Thu, 09 Mar 2017 12:09:06 -0800 (PST) Received: from ?IPv6:2a00:23c4:4bd2:6e00:5c6c:ae0f:824f:8159? ([2a00:23c4:4bd2:6e00:5c6c:ae0f:824f:8159]) by smtp.googlemail.com with ESMTPSA id 36sm9464045wrk.57.2017.03.09.12.09.04 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 09 Mar 2017 12:09:05 -0800 (PST) References: <24efb0e7-b0e3-68ed-5f42-2d5481f8f291@gmail.com> To: "internals@lists.php.net" Message-ID: Date: Thu, 9 Mar 2017 20:09:04 +0000 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] [Discussion] is_string(), string type and objects implementing __toString() From: rowan.collins@gmail.com (Rowan Collins) On 08/03/2017 23:32, Andrey Andreev wrote: > For example, a Cookie object may have the cookie attributes (domain, > path, etc.) as value objects, but they can easily be created from raw > strings, while other types would be ambiguous. > A similar effect could be desirable for HTTP headers. OK, now we have some concrete examples, thanks. I would say that in these cases, what you actually want is a much tighter contract: not just "can be converted to string", but "intended to be used in this context". It fits with what I was saying before about "if you can't name it, maybe it isn't the right abstraction". In this case, you want to accept some particular value objects but not, say, Exceptions - which are as useless for your purpose as an integer, even though (string)$foo would work fine on both. So you want objects that have promised to behave appropriately for this context; that could be as simple as: interface HTTPHeaderObject { public function __toString(); } ...with appropriate documentation that objects declaring that they implement this interface promise to behave in a specific way when cast to string. This is much clearer than detecting __toString(), which only promises "I can be cast to string" - the same promise that is made by all scalar values. In order to accept this or a string in a type hint, you need Union Types: function setHeaderValue(string|HTTPHeaderObject $value) { ... For broader use, we could perhaps have named unions: type HTTPHeaderType = union(string, HTTPHeaderObject); if ( $value instanceOf HTTPHeaderType ) { ... Meanwhile, of course, you can just use a boring old user-defined function: function is_valid_http_header_value($value) { return is_string($value) || $value instanceOf HTTPHeaderObject; } Regards, -- Rowan Collins [IMSoP]