Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:93683 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 81208 invoked from network); 1 Jun 2016 17:38:56 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Jun 2016 17:38:56 -0000 Authentication-Results: pb1.pair.com header.from=php@fleshgrinder.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=php@fleshgrinder.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain fleshgrinder.com from 77.244.243.88 cause and error) X-PHP-List-Original-Sender: php@fleshgrinder.com X-Host-Fingerprint: 77.244.243.88 mx107.easyname.com Received: from [77.244.243.88] ([77.244.243.88:54676] helo=mx203.easyname.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 82/F2-63812-EAD1F475 for ; Wed, 01 Jun 2016 13:38:55 -0400 Received: from cable-81-173-133-15.netcologne.de ([81.173.133.15] helo=[192.168.178.20]) by mx.easyname.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1b8A6T-0005pY-QB; Wed, 01 Jun 2016 17:38:50 +0000 Reply-To: internals@lists.php.net References: To: Ryan Pallas , Matt Fonda Cc: Jesse Schalken , PHP internals Message-ID: <49f1c501-5327-6576-ff2a-0513683c1db5@fleshgrinder.com> Date: Wed, 1 Jun 2016 19:38:37 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.1.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="D8bPJ9b163NtXikQBdQ2oXHs8MdgN7vPn" X-ACL-Warn: X-DNSBL-BARRACUDACENTRAL Subject: Re: [PHP-DEV] Set object properties inline From: php@fleshgrinder.com (Fleshgrinder) --D8bPJ9b163NtXikQBdQ2oXHs8MdgN7vPn Content-Type: multipart/mixed; boundary="qsVlFSMwp9v9scFnjoAL8Dv9IP4AK3htn" From: Fleshgrinder Reply-To: internals@lists.php.net To: Ryan Pallas , Matt Fonda Cc: Jesse Schalken , PHP internals Message-ID: <49f1c501-5327-6576-ff2a-0513683c1db5@fleshgrinder.com> Subject: Re: [PHP-DEV] Set object properties inline References: In-Reply-To: --qsVlFSMwp9v9scFnjoAL8Dv9IP4AK3htn Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 6/1/2016 5:47 PM, Ryan Pallas wrote: > On Wed, Jun 1, 2016 at 9:37 AM, Matt Fonda wro= te: >> >> >> Hi Jesse, >> >> It's fairly straightforward to implement a function that does this in >> userland, for example something like the following: >> >> function f($fqcn, $args) { >> $instance =3D new $fqcn; >> foreach ($args as $key =3D> $value) { >> $instance->$key =3D $value; >> } >> return $instance; >> } >> ... >> $this->fooMethod( >> $arg1, >> $arg2, >> f('FooParams', [ >> 'prop1' =3D> ..., >> 'prop2' =3D> ..., >> 'prop3' =3D> f('Obj1', [ >> 'prop1' =3D> ..., >> 'prop2' =3D> ..., >> ], >> ]) >> ); >> >> You may also use the approach Peter suggested. As such, I don't think >> introducing a new syntax for it is necessary. >> >=20 > I disagree here. This lets you add dynamic properties accidentally (by > putting too many entries in the array or misspelling a property as a ke= y), > this method does not let IDEs help you with the construction of the arr= ay, > or even what's valid for each value in that array and doesn't provide a= ny > language level protection against misspelled properties. Of course, for= the > last point, I'm assuming that this idea works like the following: >=20 > class Foo { > public $prop1; > } >=20 > new Foo () { > porp1 =3D 'foo' // undefined property error of some sort > }; >=20 >=20 >> Best, >> --Matt >> >=20 Still generically solvable in userland with creativity. :) trait ObjectFromArray { public static function fromArray(array $array, ...$params) { $object =3D new static(...$params); foreach ($array as $k =3D> $v) { if (property_exists($object, $k)) { $object->{$k} =3D $v; } else { trigger_error('fubar', E_USER_ERROR); } } return $object; } } class SomeObject { use ObjectFromArray; private $foo; } SomeObject::fromArray(['foo' =3D> 'bar', 'bar' =3D> 'foo']); --=20 Richard "Fleshgrinder" Fussenegger --qsVlFSMwp9v9scFnjoAL8Dv9IP4AK3htn-- --D8bPJ9b163NtXikQBdQ2oXHs8MdgN7vPn Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAEBCAAGBQJXTx2gAAoJEOKkKcqFPVVrh/8P/RWrhsCDbcX0TqLp4+bCSKi2 +crxcgyo5GZT+fK6KMrxf3VGnC7uzS+mg3eUBdVlLU9hW737aiK67GFpDdTp5u+j UTuhbGnlb0Xezuh49TI+9NJMi1rWv/P19wNeMrfnP1iibBldnMKqi8btA1CDO92x QOu2Z8wLCz5na6Pl3CqsSvFgdBOYEyXBcJYo2WxAamq2trz9aVq+7XHi7cKSpbg3 TipswEdQHtvTHr9FXlZFrZp2OYT7xMihyQsm/9beiNNMuLyYMyZR6ZCeOfgX05pp 6IlA43ppwaxdzGqcqlnZFKR+ipm9QMxOmpVeRdF39zcxRqVuBA3wRJRw1leNreon WICI8Sm/I6I5v9UEkjdSl0ULdZ/rI3eOLmCignH4v9IjR6SMBiK+N90K12Wx+ebO oBnFGGCydDMk4fjPPHyqQXSCrKPwlEx1R3Ax+2+VbNgRAqA08j4EALQC0FF/+nXf Dy5hSH5AfXD9SWxLcyZEbU+Iuv40dqk4UIPEeLoRUUkSJgHv0WQQHPzH9HOWrSsV PeSnG93kXNiVtXxtoyx4sw860DuFRRzuab8gwBHB6QmLGG1PAYW5Elpwd5kutETf Oz7JIAr25J0RnXdBJJ/l3QHcRHHGWUZb9dyNk/2Eb7U6vOXDUG4j+uZf0cikbhz+ yS3iBig32zGOwrimwypH =/8/c -----END PGP SIGNATURE----- --D8bPJ9b163NtXikQBdQ2oXHs8MdgN7vPn--