Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118821 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 2023 invoked from network); 15 Oct 2022 11:18:31 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 15 Oct 2022 11:18:31 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1E305180054 for ; Sat, 15 Oct 2022 04:18:31 -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,HTML_MESSAGE, 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:18:30 -0700 (PDT) Received: from [192.168.1.92] (unknown [51.179.32.0]) by mail.netik.it (Postfix) with ESMTPSA id 84DB433E02A1 for ; Sat, 15 Oct 2022 13:18:29 +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) Content-Type: multipart/alternative; boundary="------------AGINMHuz0GU0zZLHa8ZNEHSO" Message-ID: <1ebfe390-0d8c-d37a-2c15-eb1395d716f0@parentesigraffe.com> Date: Sat, 15 Oct 2022 13:18:29 +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 Content-Language: it To: internals@lists.php.net Organization: Parentesi Graffe Disposition-Notification-To: Gianni Gentile Subject: [PHP-DEV] Casting array to any class From: g.gentile@parentesigraffe.com (Gianni Gentile) --------------AGINMHuz0GU0zZLHa8ZNEHSO Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit 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 -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php --------------AGINMHuz0GU0zZLHa8ZNEHSO--