Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110851 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 55489 invoked from network); 6 Jul 2020 18:03:57 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 6 Jul 2020 18:03:57 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1E11218055C for ; Mon, 6 Jul 2020 09:54:26 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-il1-f179.google.com (mail-il1-f179.google.com [209.85.166.179]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 6 Jul 2020 09:54:25 -0700 (PDT) Received: by mail-il1-f179.google.com with SMTP id i18so33509375ilk.10 for ; Mon, 06 Jul 2020 09:54:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=VFqokiJCCXEbHAVhNKhartGrObf9UjrBLDHhcg2Ds/o=; b=iP0UNUQlO77iYUJdte5KSjJW3hFmxs2QmORJGGpwVic3wERH//Mc32Ui4+dCpGT7Ly UQ0FIv6x0pP9K2U0oJLDz+Atlab3caRUcGiC1qniL+xobWtXRTcZAPdfjC8uBAspzAoW VSMq5ukpDd2I1W5iF8lChgwm24WMYDFmpki9WJ9ZVqRccvmvIPcI0/H5+uWhEyLi1DFy tS0zSYzwo19upQVdEMbY6QAZOZuY/tDxlzGM6YIbf8MFn7r0cWKaYnnzpdaHfUlN+fYE ZuIbdNF1YQgtdNXIlwZJf90OzR6kSWmhxKE/KN82wHrjjzsQKSKJa02zFk2qSEdHGTl/ mWDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=VFqokiJCCXEbHAVhNKhartGrObf9UjrBLDHhcg2Ds/o=; b=q0qZORUz94A3OpaNXFXGQPMknkmRZ6x+nTiCksXtgyGQWqVFzjAK7jO+v8VYZRmdXM txoLAg7L5rwOk5gnCgr0Jo/yxTMx/P39QE0xj6rMe61YUmEiFI66XjPGH8SYFMNfoLQg XIwQLJqN4JEM23LFIEw7mHIESY/5uTHbqbWEAzC+nAPFWyGPYXVGT8AzwQ4IqnN+jvQb tU2mqvBE2ZuP0pdSPZdDFkxhQE4NSdg8TC5NBfiFfhKNoIAlqW3uMbLLOZs9I1n4yeDP CRYWlbEg69NNdw/utUEa7mp5N4FGKR3fETJl0W1KVI5OZQGtSYymYO/olikPdSPbR95/ i02Q== X-Gm-Message-State: AOAM531V7O94abt3wkuiW5Wm3BS86E067+uHJzwfCzD+Sn3ZraToKed7 DZ4T0qftoQ/geGSrE87/2MSQpFLjn04idv7J+B1kih3CDiR+pQ== X-Google-Smtp-Source: ABdhPJwcby6wH/W/yzctBtazHrw/hQNOo5D0Eu4iRWBL6QlS5nruto55B1473kLaLmmau09lxWYnaXOYcO7NL2WBDrw= X-Received: by 2002:a92:cd04:: with SMTP id z4mr31813559iln.165.1594054461379; Mon, 06 Jul 2020 09:54:21 -0700 (PDT) MIME-Version: 1.0 References: <0431210B-417F-4576-BF28-E51ECFA8F3EF@joshbruce.dev> In-Reply-To: <0431210B-417F-4576-BF28-E51ECFA8F3EF@joshbruce.dev> Date: Mon, 6 Jul 2020 18:54:10 +0200 Message-ID: To: Josh Bruce Cc: PHP Internals List Content-Type: multipart/alternative; boundary="000000000000ec58eb05a9c8b9e9" Subject: Re: [PHP-DEV] Proposal: A way for classes to define a response to any primitive type cast From: ocramius@gmail.com (Marco Pivetta) --000000000000ec58eb05a9c8b9e9 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hey Josh, Similar proposals were raised recently for `__toArray()`: see https://externals.io/message/108369#108369 I'd endorse avoiding object-to- casts via cast operations: they are a good source of bugs. My rationale for the discouragement of magic cast methods is explained with some code examples at https://github.com/ShittySoft/symfony-live-berlin-2018-doctrine-tutorial/pu= ll/3#issuecomment-460441229 Greets, Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/ On Mon, Jul 6, 2020 at 4:17 PM Josh Bruce wrote: > Apologies, first time, still learning and have no historical context - > timing is what it is. > > Proposal type: Concept > > Implementer: Unknown, fallback to me after slow learning > > Presumed simple implementation as the pattern should already be in place: > > Cast of (string) -> __toString() - already implemented > > Cast of (bool) -> __toBool() > > Cast of (int) -> __toInt() > > Cast of (object) -> __toObject() > > =E2=80=A6and so on. > > Alternative implementation using an interface: > > \CastableTo{type} > > Extreme implementation: > > Make both of the previous available. > > Known unknowns: > > - Level of interest in something like this in the PHP community outside o= f > one upvote on a Stackoverflow question I submitted: > https://stackoverflow.com/questions/62747837/php-7-tostring-or-other-inte= rface-for-boolean-values > < > https://stackoverflow.com/questions/62747837/php-7-tostring-or-other-inte= rface-for-boolean-values > > > - Whether this conversation or proposal has already occurred. > - The historical conversation leading to the creation of the __toString() > method (and stopping there). > - The current conversation around using magic methods in general and > specifically when defining a cast response. > - Enough details on PHP internal development to implement myself (this is > the first time in 20 years I felt I couldn=E2=80=99t comfortably accompli= sh what I > wanted to, thanks for that). > - What potential collisions could occur if someone (me) wanted to > implement all possibilities in a single class. > - Right now __toBool() would be of the most personal benefit - if proposa= l > is converted to an RFC, would it be better (faster/smoother) to do one RF= C > for all the cast possibilities - or to have an RFC for each cast separate= ly? > > Thank you for the consideration, information, and feedback. > > Cheers, > Josh > > ps. Also, apologies again for walking into a new space on a mission, not > my normal approach. > > pps. The primary project inspiring the proposal: > https://github.com/8fold/php-shoop > > > --000000000000ec58eb05a9c8b9e9--