Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:86732 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 1216 invoked from network); 17 Jun 2015 12:24:25 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Jun 2015 12:24:25 -0000 Authentication-Results: pb1.pair.com smtp.mail=johannes@schlueters.de; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=johannes@schlueters.de; sender-id=unknown Received-SPF: error (pb1.pair.com: domain schlueters.de from 217.114.215.10 cause and error) X-PHP-List-Original-Sender: johannes@schlueters.de X-Host-Fingerprint: 217.114.215.10 mail.experimentalworks.net Received: from [217.114.215.10] ([217.114.215.10:48668] helo=mail.experimentalworks.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 43/B1-23581-5F661855 for ; Wed, 17 Jun 2015 08:24:22 -0400 Received: by mail.experimentalworks.net (Postfix, from userid 1003) id E49654BF0C; Wed, 17 Jun 2015 14:25:04 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on km31408.keymachine.de X-Spam-Level: * X-Spam-Status: No, score=1.4 required=4.0 tests=ALL_TRUSTED, DNS_FROM_AHBL_RHSBL autolearn=no version=3.3.2 X-Spam-HAM-Report: * 2.4 DNS_FROM_AHBL_RHSBL RBL: Envelope sender listed in dnsbl.ahbl.org * -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP Received: from [192.168.2.34] (ppp-93-104-12-204.dynamic.mnet-online.de [93.104.12.204]) (using TLSv1.2 with cipher DHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: johannes@schlueters.de) by mail.experimentalworks.net (Postfix) with ESMTPSA id C73244BF0C; Wed, 17 Jun 2015 14:25:02 +0200 (CEST) Message-ID: <1434543855.21120.37.camel@kuechenschabe> To: Lorenzo Fontana Cc: internals@lists.php.net Date: Wed, 17 Jun 2015 14:24:15 +0200 In-Reply-To: References: Content-Type: multipart/signed; micalg="pgp-sha1"; protocol="application/pgp-signature"; boundary="=-7sX8k/5hbiEuWCLe/pc7" X-Mailer: Evolution 3.10.4-0ubuntu2 Mime-Version: 1.0 Subject: Re: [PHP-DEV] Initializer List From: johannes@schlueters.de (Johannes =?ISO-8859-1?Q?Schl=FCter?=) --=-7sX8k/5hbiEuWCLe/pc7 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, 2015-06-17 at 10:03 +0200, Lorenzo Fontana wrote: > - In C++ using initializer list is used over assignment also for > performance reasons but right now I can't say if this can be achieved in > PHP or not. >=20 > Initializer lists in C++: > http://en.cppreference.com/w/cpp/language/initializer_list Mind that C++ doesn't do this to make code "nice" or "short" or whatever in that area but primarily due to need of control. Not having initializer lists means that object members in C++ have to be default constructible. Given C++ code like this struct C { SomeType a; C(const SomeType& a) { this->a =3D a; } } first calls the default constructor SomeType::SomeType() for a and then the assignment constructor (SomeType::operator=3D(const SomeType&) or similar)=20 Another reason why C++ needs this ist const-correctness. In the example above it will fail if a is marked as const as the default constructor will initialize and assignment on const objects isn't valid. In PHP however the default constructor equivalent is setting the type to NULL which is (almost) for free while allocating the variable. Thus the need isn't there. You also write > public function __construct() : host("127.0.0.1") {}; as example. But that ignores PHP's way to define default values: class Foo { private $host =3D "127.0.0.1"; } would be the PHP version to what you propose. At the same time I am confussed, by using a literal string there I assume you want to allow expressions there, but in other examples, like > public function __construct($name, $surname, $age) : name(name), > surname(surname), age(age) {}; you are not handling expressions (unless the constructor arguments are being ignored and properties are being initialized using the global constants name, surname and age, which I assume you didn't intend) You are also not discussing inheritance. Can I do class Base { /* .... */ } class Extended extends Base { public function __construct() : parent(42) {} } how does that relate to parent::__construct()? In summary: I consider this syntactic sugar which in my opinion doesn't increase readability and in other languages solves an issue PHP doesn't have. johannes --=-7sX8k/5hbiEuWCLe/pc7 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part Content-Transfer-Encoding: 7bit -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.22 (GNU/Linux) iQEcBAABAgAGBQJVgWbvAAoJEH3sTmn8nIPXxsEIAMZ7E+kYoARBBhyk5+mlbyah 8GPvJrJziiCUJbXDydMygrb0XAE6JOk5s3SHcCw3LsemaHvHBfBxSXSaq5KNe3/o sRca/Vtc3SrcL4KhAOw/yEOsmCKkUdZWoPL7+wBTDUAAqnTx1abEDcyczgh1g9a0 NFoDpncJNfPPkK+o30R1KEqOAQo9K/97jCaF1nyJXvWHC/6ti8e+SgrN7H1d06ex 7O/8FGfHWfwiemmJtxLBXSCz2VJrii1+YliyiuQgLKiEU8EieWJCxFwfGc9y/2Bl FUfyefjprTVT5evBUSf8k9XimE1YSPybjin9XhQfV5uVjqoPr9MWwdGfkvseH6o= =crTT -----END PGP SIGNATURE----- --=-7sX8k/5hbiEuWCLe/pc7--