Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98424 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 6852 invoked from network); 8 Mar 2017 12:25:59 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 8 Mar 2017 12:25:59 -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.174 as permitted sender) X-PHP-List-Original-Sender: narf@devilix.net X-Host-Fingerprint: 74.125.82.174 mail-ot0-f174.google.com Received: from [74.125.82.174] ([74.125.82.174:33438] helo=mail-ot0-f174.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id CE/D2-06022-558FFB85 for ; Wed, 08 Mar 2017 07:25:59 -0500 Received: by mail-ot0-f174.google.com with SMTP id 19so29704828oti.0 for ; Wed, 08 Mar 2017 04:25:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=devilix.net; s=google; h=mime-version:from:date:message-id:subject:to :content-transfer-encoding; bh=2iNJL0b1RK1DTqzBnEz5acKRX5feDa6ufEcjNx+PG1w=; b=L/lPbSRkosi/exisS2Xg7zBst1MBb4pzISif5Q5SXYFM74mMe/0ep1xBkvYb72ddfl OIBt/O+IsvaywvW5L2PldqqnIzqWC7tk4JE9fOsU4u4I1P3TTF5poXE/8gL0HOyQrYZz qUoS3DDm+rJ45iIkBya4jbWbHjUcaTffx8/0E= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to :content-transfer-encoding; bh=2iNJL0b1RK1DTqzBnEz5acKRX5feDa6ufEcjNx+PG1w=; b=EudLduPLBje2Ne6rgDE922Gi8ktllYu/9LRlhT3VRJSwTC2hJuZBlvkqEBbr4/p0hc KyF4mMSOqYL0Xbta32GRMt/hUIswOpdM7r0RFiWZ0D2jVsknjvxnFbq/neFA/1Fm3fCO GGJvvi6JFxEKtSf9kJaILQmP/e3HUMMyEOyGmReQVSdhh3DMW3jBHKt72zI1KF4IChUJ dsIz+LRrhQzgZubLXzuds+tfvIp2LfqMBy9ZtEI6AIBVb26hL4gGFukzvmkYeTh4Zmci iow31Cv3AtKubJlo+td+LbamAu7hw5Xq6hpLQTl3p7nG0t8v89fIvv/a8W1YcVXUC/rM 7nBg== X-Gm-Message-State: AMke39mrksxQ34oBbrQdFaLEWDEOzICPYHTZyJrW61BUS9kEBMsWqdlDDgl1QOvqf8dEGn48vb0HxcdfNW8yig== X-Received: by 10.157.27.154 with SMTP id z26mr3567766otd.181.1488975954960; Wed, 08 Mar 2017 04:25:54 -0800 (PST) MIME-Version: 1.0 Received: by 10.182.242.83 with HTTP; Wed, 8 Mar 2017 04:25:54 -0800 (PST) Date: Wed, 8 Mar 2017 14:25:54 +0200 Message-ID: To: "internals@lists.php.net" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Discussion] is_string(), string type and objects implementing __toString() From: narf@devilix.net (Andrey Andreev) 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=3D0 mode (otherwise any scalar type is accepted) - Can be used for looser string parameter validation in strict_types=3D1 mode (__toString() objects aren't accepted there) - Regardless of the last 2 points, it is intentionally not limited to parameter types * 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 fla= g to a function directly results in the fact that the function violates the= single responsibility principle. What we actually need to make this work i= s 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 acce= pted, but did not find the time yet. > > Closing note: these pseudo-types are necessary in PHP because it has no c= oherent type system, and there is nothing we can do about this in short ter= m. 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.