Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118826 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 18507 invoked from network); 15 Oct 2022 14:33:45 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 15 Oct 2022 14:33:45 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 93EC4180538 for ; Sat, 15 Oct 2022 07:33:44 -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.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS29838 64.147.123.0/24 X-Spam-Virus: No X-Envelope-From: Received: from wout3-smtp.messagingengine.com (wout3-smtp.messagingengine.com [64.147.123.19]) (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:33:43 -0700 (PDT) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.west.internal (Postfix) with ESMTP id 2958D32008FD for ; Sat, 15 Oct 2022 10:33:41 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute1.internal (MEProxy); Sat, 15 Oct 2022 10:33:41 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-transfer-encoding:content-type :date:date:from:from:in-reply-to:in-reply-to:message-id :mime-version:references:reply-to:sender:subject:subject:to:to; s=fm3; t=1665844420; x=1665930820; bh=xeO9Mp1pGBdI6om26zOSzsHQz mGnB2lGptvT2Gg34AU=; b=CIm/7gbmOH3UITLKpWfD6w0y/1gzGzqcR9p/b25me xYuoRt8Wz12mMHgD1A2tZM4efIU17MbcWd5x5X1D5J6EVsEwzS3BYtP/7Raxr2Oz Shrb0e+I+XDAB+hI4KlRvcUvZHKESdQYVvsS4GIUEm9XCPGlXhfMv2XRTvK40S83 YTMRelh4qliLNcDDXOYVvKAyd3Fl6A5zL+pCPVM+97yR8/9CSasLiwRPj+gWnOUG Fzz/T8mg0cPfyj9lLoqm3DVlAYCEg4T+EypkyH2on00QE8RFRDLqT7DhyQZxrqYH DyNyjsTwJ4q08idNTeZBf96k+bBK9JeByzZKpx0XO8nSQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:date:feedback-id:feedback-id:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:sender :subject:subject:to:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; t=1665844420; x=1665930820; bh=x eO9Mp1pGBdI6om26zOSzsHQzmGnB2lGptvT2Gg34AU=; b=fG1A16ZLktdoC7EOQ NENgJLsWWrodoswfEYiC/Yo69RlMOsuQn2EcupsSefwqhRLycz5pPTFihw2lhbbm Nc0vdT+zDK5LXqX8nvnDIqrys3MgeXSSX6KjZE/OFFoXZSVLYDRFq2w55JxihkvE KDHYrz37sy06aZGNLMh/CIrdnjUN1XO5G7Nxd8A3579SUhEMxT7HfPlGjs9+p6Xh h2GlrHGXyF14cFfYlhAdWVcEhaAlxnjRD5cjmwBRYa2KHEItsxNgfV/1cB26d+ZV /TpBRJVPmyxR1S1dC3G4J0c3oribItnpOSwdSYtllch0cuqCijmq0Sf6TGutAQLy tYVBQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedrfeekgedgjeekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgfgsehtqhertderreejnecuhfhrohhmpedfnfgr rhhrhicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtg homheqnecuggftrfgrthhtvghrnhepffffffejffdugfegvedviedttedvgfejffefffej leefjeetveehgefhhfdvgfelnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpe hmrghilhhfrhhomheplhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhm X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 70F2A1700083; Sat, 15 Oct 2022 10:33:40 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.7.0-alpha0-1047-g9e4af4ada4-fm-20221005.001-g9e4af4ad Mime-Version: 1.0 Message-ID: In-Reply-To: <1ebfe390-0d8c-d37a-2c15-eb1395d716f0@parentesigraffe.com> References: <1ebfe390-0d8c-d37a-2c15-eb1395d716f0@parentesigraffe.com> Date: Sat, 15 Oct 2022 09:33:15 -0500 To: "php internals" Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Casting array to any class From: larry@garfieldtech.com ("Larry Garfield") On Sat, Oct 15, 2022, at 6:18 AM, Gianni Gentile wrote: > Hi, > > in my every day experience, using custom DTO classes and different API= ,=20 > I often write code to instantiate my objects from associative arrays. > > What are your thoughts on introduce the `(AnyType)` cast to instantiat= e=20 > objects from associative array (or object properties also)? > > In my proposal, if AnyType implements __set_state(), casting should be=20 > syntactic sugar for: > > =C2=A0=C2=A0=C2=A0 AnyType::_set_state((array) $data) > > just for (useless) example, consider DateTime or DateTimeImmutable (bo= th=20 > have __set_state() implementation); when applied to objects of that=20 > type, we can write: > > =C2=A0=C2=A0=C2=A0 $dt =3D new DateTime(); > > =C2=A0=C2=A0=C2=A0 $dtArray =3D (array) $dt; // 3 elements array > > =C2=A0=C2=A0=C2=A0 $copyOfDt =3D (DateTime) $dtArray; // or simply $c= opyOfDt =3D=20 > (DateTime) $dt; > > In case AnyType does not implements __set_state(), casting should crea= te=20 > a new instance, assigning values to properties, taken from array value= s=20 > with corresponding key (in a similar way class objects are created whe= n=20 > row is fetched from database specifying PDO_FETCH_CLASS) eventually=20 > invoking __set() magic method for properties not declared; > > In this scenario, > > =C2=A0=C2=A0=C2=A0 (object) ['one' =3D> 1, 'two' =3D> 2] > > and > > =C2=A0=C2=A0=C2=A0 (StdClass) ['one' =3D> 1, 'two' =3D> 2] > > do exactly the same work. > > > Reasons: > > - clear code > > - array to object conversion (casting) would be the simmetrical=20 > counterpart of object to array conversion Most DTOs have a 1:1 match from constructor to properties. In current P= HP versions, that means they often look like this: class Point { public function __construct( public readonly int $x, public readonly int $y, public readonly int $z, ) {} } Which means, in current PHP versions, you can use named args and splat t= o create one from an array. $data =3D ['x' =3D> 1, 'y' =3D> 2, 'z' =3D> 3]; $point =3D new Point(...$data); No casting necessary. True, that's not true of all such classes, but certainly the plurality. = In the cases where it's not, an explicit named constructor (via a Trait= or otherwise) is a more self-documenting approach. --Larry Garfield