Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118823 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 13442 invoked from network); 15 Oct 2022 14:12:41 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 15 Oct 2022 14:12:41 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8CB871804B3 for ; Sat, 15 Oct 2022 07:12:39 -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,NICE_REPLY_A, 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: AS8560 212.227.0.0/16 X-Spam-Virus: No X-Envelope-From: Received: from mout.kundenserver.de (mout.kundenserver.de [212.227.17.10]) (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:12:38 -0700 (PDT) Received: from [192.168.178.22] ([85.212.95.14]) by mrelayeu.kundenserver.de (mreue109 [213.165.67.113]) with ESMTPSA (Nemesis) id 1MhlCg-1pN9Jk0nuy-00drt7; Sat, 15 Oct 2022 16:12:36 +0200 Message-ID: <425af4f0-cbcd-01a7-a400-a818e09b447e@nunninger.info> Date: Sat, 15 Oct 2022 16:12:35 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.3.1 Content-Language: de-DE-frami, en-US To: Gianni Gentile , internals@lists.php.net References: <1ebfe390-0d8c-d37a-2c15-eb1395d716f0@parentesigraffe.com> In-Reply-To: <1ebfe390-0d8c-d37a-2c15-eb1395d716f0@parentesigraffe.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Provags-ID: V03:K1:LhnQGzfNGxy9p+VN3g3nq2r60TeHf4gttB/41WAt7aW4x71Waaw bb1lHRcIMghs/tw2AGxLUgNlwevziPOZi6wCoMUrYGt2MYVzG+/hsdIuDsWEEj81wqVN2Pn XVsXNb1RCTQryoM2Xlh5i3lDjiN4es5KTV6UZ/8IX/2XWiyYyUGXGyO6nzmIzPQg//cqMCR bRvRQJjNkqkllD/dQDtBg== X-UI-Out-Filterresults: notjunk:1;V03:K0:TulcHI/Nh60=:o1NDKwE5Gf2VQLGFJRFYg7 JgqsFC93cCHiukyy7iM/YLT2sW86FcKSAcMMdoVcbiogxPgttk3ObZqU+h6ehINQz/fz+QP7U JxhuC6vDiJl7+8ScvRvhfTatNyBeW6dRijzGEjVAvbBx6vwS/cdsaFoA4LHB2RyI89SpIoSYZ VJUgCZp4ExaJeMDaRZVS0hIW/yF/KfxKZaug/fhNpMZ+d6yPOXekgEsScIZD5HNWXKmPHqCLH uvOIM8JXcEYAjCJc4ceit0zjhDms2GiYe8/oE361KbGmDuHlU5K3TyLAWwNCYFlPqpGqm+vXS ijXeBYdDMFfc2pn/4F/aZDjMNGdCjf/XaWsUnhTh7G8BHyngQk2NuPL2sbs8Cjgw/X+Y3ZP5h hPGB/zdd1OsMV1fAXgB0k4kZs0h+H0cCyf8i/zf7Q87++tK76PdAXLdQjN3SpCQBCHLNZdkb9 yb5JHQfwS7LazcpnM0iAyqB6fFskGdKvxaa4iT+GTMHjwksa9dW1gNzDeSNyEr59jAKKBymQt utKBaVkrbgsV1wHb8RjvpTC1q+SqeWaX7AJvFOVSUVEKoa+4CJslOlcbcvdzpc54aBOEdrPVj s5VBVivtM6HhGiSy7UVxjAVFlO4BCpndVN9Tc867LzDiff68p2v3TJfbN/g2WaAbcj8L4TXKJ KntYHmFwdsY+wk10DYOpYvtKGzxl2JhpHrRmmB3bNVDzQhgS590+2QUStL3tRBe2p4TMWWKg5 MbKPkKz979BkXYYxgeLI8GFJiNJZHWgS+5U5gpQ1qIFLyyBb742d2LVaQ1P3Xyd+Oer5pK3Be PJOIGh1wp+dIcpW2osaCG53Z1ivpQ== Subject: Re: [PHP-DEV] Casting array to any class From: thomas@nunninger.info (Thomas Nunninger) Hi, Am 15.10.22 um 13:18 schrieb 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 I see your use case and I often have similar needs. Some thoughts: * Are there any restrictions regarding the matching of array keys and the class attributes? What if there are additional keys in the array that do not have corresponding attributes? What if there are some attributes in the class that do not exist in the array? * Can I forbid a class to be created by casting from an array? When I think about domain entities/aggregates, such a casting would allow to create an invalid state of the object without any checks. (Okay, we have similar issues when recreating them from a database via some ORM.) BTW: Will dump with modified array keys: /home/thomas/test.php:12: array(3) { '\0A\0myPrivate' => string(7) "private" '\0*\0myProtected' => string(9) "protected" 'myPublic' => string(6) "public" } So it's not as symmetric as intended. Regards Thomas