Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:59904 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 72357 invoked from network); 13 Apr 2012 19:29:35 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Apr 2012 19:29:35 -0000 Authentication-Results: pb1.pair.com smtp.mail=dsnytkine@Ultralogistics.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=dsnytkine@Ultralogistics.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain Ultralogistics.com from 64.197.110.172 cause and error) X-PHP-List-Original-Sender: dsnytkine@Ultralogistics.com X-Host-Fingerprint: 64.197.110.172 thrud.alliantinternet.com Received: from [64.197.110.172] ([64.197.110.172:34039] helo=thrud.alliantinternet.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B9/6A-35770-E9E788F4 for ; Fri, 13 Apr 2012 15:29:35 -0400 Received: by thrud.alliantinternet.com (Postfix, from userid 1001) id 40ACC174321; Fri, 13 Apr 2012 15:29:32 -0400 (EDT) To: "'Etienne Kneuss'" Cc: "'PHP Internals'" References: <01ed01cd19aa$58810e80$09832b80$@alliantinternet.com> In-Reply-To: Date: Fri, 13 Apr 2012 15:29:30 -0400 Message-ID: <01ee01cd19ab$bf2c83c0$3d858b40$@alliantinternet.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Thread-Index: Ac0Zq2IgoVkSPbEmSlOkvRLcyuOr1gAADpwg Content-Language: en-us Subject: RE: [PHP-DEV] Ability to assign new object to a class property. From: dsnytkine@Ultralogistics.com ("Dmitri Snytkine") But why is it possible to assing a new array to a property like that = but not a new instance of some class? Isn't a new array also requires some type of initialization? Dmitri Snytkine Web Developer Ultra Logistics, Inc. Phone: (888) 220-4640 x 2097 Fax: (888) 795-6642 E-Mail: dsnytkine@ultralogistics.com Web: www.ultralogistics.com "A Top 100 Logistics I.T. Provider in 2011" -----Original Message----- From: ekneuss@gmail.com [mailto:ekneuss@gmail.com] On Behalf Of Etienne = Kneuss Sent: Friday, April 13, 2012 3:27 PM To: Dmitri Snytkine Cc: PHP Internals Subject: Re: [PHP-DEV] Ability to assign new object to a class property. Hi, On Fri, Apr 13, 2012 at 21:19, Dmitri Snytkine wrote: > I always wondered why can't we do something like this in php > > class MyClass{ > > private $storage =3D new ArrayObject(); > > public function __construct($v){ > // whatever > } > > // rest of class > > } > > Why can't we create a new object and assign it to property like this? > > Then when a new instance of MyClass is created the $storage variable = is > automatically assigned a new ArrayObject. > Somethink like this is valid, possible and commonly used in Java, why = not in > php? > > Has anyone already asked for this to be valid syntax in php? Sure, people have asked for that. It is definitely possible in theory, but requires quite some work: It requires to add an initialization phase to the class. In other languages, it is typically done by automatically adding the initialization code in the constructors. We cannot do that in PHP because calling parent constructors is not mandatory. In other words, in order to allow for this, we need to add an initialization phase before calling the constructors. Allowing this syntax for classes properties would also require adding an initialization phase, and it is not entirely clear when to run it (i.e. when the class is first used or when it is loaded). Best, > > Dmitri Snytkine > Web Developer > Ultra Logistics, Inc. > Phone: (888) 220-4640 x 2097 > Fax: (888) 795-6642 > E-Mail: dsnytkine@ultralogistics.com > Web: www.ultralogistics.com > > "A Top 100 Logistics I.T. Provider in 2011" > > > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > --=20 Etienne Kneuss http://www.colder.ch