Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:59905 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 73943 invoked from network); 13 Apr 2012 19:36:07 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Apr 2012 19:36:07 -0000 Authentication-Results: pb1.pair.com header.from=ekneuss@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ekneuss@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.210.45 as permitted sender) X-PHP-List-Original-Sender: ekneuss@gmail.com X-Host-Fingerprint: 209.85.210.45 mail-pz0-f45.google.com Received: from [209.85.210.45] ([209.85.210.45:46484] helo=mail-pz0-f45.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 1F/BA-35770-520888F4 for ; Fri, 13 Apr 2012 15:36:05 -0400 Received: by dacx6 with SMTP id x6so4283515dac.18 for ; Fri, 13 Apr 2012 12:36:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=sYGHUBpgtWhxHTJzZTPlRDJLTJXoKpRfLd0JTSflV2A=; b=lnt+fNOOBLcTsVy0lP8seBfxLehwOaWs2D1PiwAdYM6DKfefcSZsRJJMS5GXRGrQTC 0S9Ed/MVTW61s86SlHROEwnUzsiq5v/Z05d2mJ3ezR04ztapQl5fIl/uw6UKuuHCn0++ eSSSNiMws66VZR2BIzgiRqCZhdKM8hwXCK7lwxp2P+xyiVKcWWFNLyuXBvEYbJyQBElh QFbCYO+WpehA2nIPmVWkeEWk/hWpAzUCyDffANtp7Mhtj8FA81ElQire6p/PBqMB9kHO fMPTNeh4T3jbsNo07KU20a9a/teCsIGm+pY2zsF1MClwkb69A5AXLZGtBJfny+KRndDm Dvug== MIME-Version: 1.0 Received: by 10.68.225.132 with SMTP id rk4mr5935034pbc.157.1334345762307; Fri, 13 Apr 2012 12:36:02 -0700 (PDT) Sender: ekneuss@gmail.com Received: by 10.68.72.6 with HTTP; Fri, 13 Apr 2012 12:36:02 -0700 (PDT) In-Reply-To: <01ee01cd19ab$bf2c83c0$3d858b40$@alliantinternet.com> References: <01ed01cd19aa$58810e80$09832b80$@alliantinternet.com> <01ee01cd19ab$bf2c83c0$3d858b40$@alliantinternet.com> Date: Fri, 13 Apr 2012 21:36:02 +0200 X-Google-Sender-Auth: 2vEwgX_WnxQ3olgfDZkvjWDhBwo Message-ID: To: Dmitri Snytkine Cc: PHP Internals Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Ability to assign new object to a class property. From: colder@php.net (Etienne Kneuss) On Fri, Apr 13, 2012 at 21:29, Dmitri Snytkine wrote: > But why is it possible to assing =C2=A0a new array to a property like tha= t but not a new instance of some class? > Isn't a new array also requires some type of initialization? Only static values are allowed, which means that the value requires no code to execute. > > 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 K= neuss > 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 no= t 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 >> > > > > -- > Etienne Kneuss > http://www.colder.ch > --=20 Etienne Kneuss http://www.colder.ch