Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118820 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 504 invoked from network); 15 Oct 2022 11:13:58 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 15 Oct 2022 11:13:58 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 959AD180054 for ; Sat, 15 Oct 2022 04:13:57 -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=-0.0 required=5.0 tests=BAYES_40,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS60460 46.231.27.0/24 X-Spam-Virus: No X-Envelope-From: Received: from mail.netik.it (mail.netik.it [46.231.27.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 15 Oct 2022 04:13:56 -0700 (PDT) Received: from [192.168.1.92] (unknown [51.179.32.0]) by mail.netik.it (Postfix) with ESMTPSA id 2C09F33E0256 for ; Sat, 15 Oct 2022 13:13:52 +0200 (CEST) Authentication-Results: mail.netik.it; spf=pass (sender IP is 51.179.32.0) smtp.mailfrom=g.gentile@parentesigraffe.com smtp.helo=[192.168.1.92] Received-SPF: pass (mail.netik.it: connection is authenticated) Message-ID: Date: Sat, 15 Oct 2022 13:13:19 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.3.3 To: internals@lists.php.net References: <1aa20877-3365-0ace-25d2-1cb1be8f3bcc@gmail.com> <55686BD4-4C67-4A4F-8BE4-BDF0149296FB@gmail.com> Content-Language: it Organization: Parentesi Graffe Disposition-Notification-To: Gianni Gentile In-Reply-To: <55686BD4-4C67-4A4F-8BE4-BDF0149296FB@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Casting array to any class From: g.gentile@parentesigraffe.com (Gianni Gentile) Hi, in my every day experience, using custom DTO classes and different API, I often write code to instantiate my objects from associative arrays. What are your thoughts on introduce the `(AnyType)` cast to instantiate objects from associative array (or object properties also)? In my proposal, if AnyType implements __set_state(), casting should be syntactic sugar for:     AnyType::_set_state((array) $data) just for (useless) example, consider DateTime or DateTimeImmutable (both have __set_state() implementation); when applied to objects of that type, we can write:     $dt = new DateTime();     $dtArray = (array) $dt; // 3 elements array     $copyOfDt = (DateTime) $dtArray; // or simply $copyOfDt = (DateTime) $dt; In case AnyType does not implements __set_state(), casting should create a new instance, assigning values to properties, taken from array values with corresponding key (in a similar way class objects are created when row is fetched from database specifying PDO_FETCH_CLASS) eventually invoking __set() magic method for properties not declared; In this scenario,     (object) ['one' => 1, 'two' => 2] and     (StdClass) ['one' => 1, 'two' => 2] do exactly the same work. Reasons: - clear code - array to object conversion (casting) would be the simmetrical counterpart of object to array conversion Thanks, Gianni