Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118824 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 15266 invoked from network); 15 Oct 2022 14:22:26 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 15 Oct 2022 14:22:26 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4ED601804A9 for ; Sat, 15 Oct 2022 07:22:25 -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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE 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-vs1-f53.google.com (mail-vs1-f53.google.com [209.85.217.53]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 15 Oct 2022 07:22:24 -0700 (PDT) Received: by mail-vs1-f53.google.com with SMTP id k6so7578758vsc.8 for ; Sat, 15 Oct 2022 07:22:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=basereality-com.20210112.gappssmtp.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=XR0zskrqKSvEa3lv9PwM5mHM1oiJthAqtK4I2bPe+lo=; b=hurtgLfgJNMBEGfUkTXFODtoOTTYKCziWZUr0M8TQsBqXmfkAIG3zt2Yolp6569OIc uE4YnxhU20thWIVNcD0Dl6kaVRPBCU5vL3WzTu8e8A9Llfs59fQT7MjXXb4Ita+EN26E yOFL+caAVT2iRs+fjlK/NbUFXulWiXN1PB3pYBEsw+7vD4wAQ2bADF8jouGMvu7MLBjb aKYB4sTmX8k35gOQTe4AIoO6Hp1/wEIZkpVjxG6uQB1+KUpi/OMJb4HZZGingAhC0l4o pYUfCF+8plBdkCjyx3Jb/XtpTpzWoS1uuC1RYTTbITBDXC5zGcLgnpPIkahg88iCRfR+ 4KvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=XR0zskrqKSvEa3lv9PwM5mHM1oiJthAqtK4I2bPe+lo=; b=1MWDhckx5PKfmIz+EgfmwoPuiXPS8VEUlwtTS8fdh8k0b6DW4Yvuy9cWPS59ym2gSD uynj2ux/9ZqUz1hTKmr7tgdB7p/tvPNDmK2zc6ST4TA2fJytMOdisHiDEg+Z6pUuj3DU eAMjOD7X4eFjUZ5BV6yEc55BCVjKjeMuMqaVtigVlJxbU6T8/iJxiYp9uS9sZXr1mFwe ySFvfblICvtfhEmZ0Shk//QRoLMVJH7FRoA2BhO7AvOq8dERXidZiQlbYSIsfwRPUNr9 wcO5SOrSbQYBK9PCz9R/5B5ZPqqoyqOiIGJjHwZH8MICkP3LNbA4PhHkeR8l7v9vLYm2 QJOA== X-Gm-Message-State: ACrzQf1pDqnYgRCF0VBdbm8eNQ21oSOV33EWy+/pmY6+tK95+YaKtsXD w3kLz6MO803LQhcYzmgdAugNZnKFWzGbcjLG2fV4pfw66JTlXf4C X-Google-Smtp-Source: AMsMyM5G10H6+iOnluOza0KRUR7NVsHRgaRVTso2qiWH5ITVd8EhEdDPA5WiC422IAcl1EgiAbts+Vb9a+kQq4QPYjQ= X-Received: by 2002:a67:e00d:0:b0:3a7:6660:a13d with SMTP id c13-20020a67e00d000000b003a76660a13dmr1077714vsl.8.1665843743960; Sat, 15 Oct 2022 07:22:23 -0700 (PDT) MIME-Version: 1.0 References: <1aa20877-3365-0ace-25d2-1cb1be8f3bcc@gmail.com> <55686BD4-4C67-4A4F-8BE4-BDF0149296FB@gmail.com> In-Reply-To: Date: Sat, 15 Oct 2022 16:22:13 +0200 Message-ID: To: Gianni Gentile Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Casting array to any class From: Danack@basereality.com (Dan Ackroyd) On Sat, 15 Oct 2022 at 13:14, Gianni Gentile wrote: > > What are your thoughts on introduce the `(AnyType)` cast to instantiate > objects from associative array (or object properties also)? It seems a pretty bad idea. In particular, it makes for hard to maintain code as it doesn't give a place for error checking to occur. Though I do actually use that pattern in code. Below is a trait that accomplishes it that should only be used in prototype code. Having it as a trait means at least I can debug through the initialization and see if it's missing properties from the array, But in general, you'd really need to make a strong argument for why it should be in core, not just why anyone would be against it. cheers Dan Ack trait FromArray { /** * @param array $data * @return static * @throws \ReflectionException */ public static function fromArray(array $data): self { $reflection = new \ReflectionClass(__CLASS__); $instance = $reflection->newInstanceWithoutConstructor(); foreach ($instance as $key => &$property) { $property = $data[$key]; } return $instance; } }