Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:59903 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 71052 invoked from network); 13 Apr 2012 19:26:55 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Apr 2012 19:26:55 -0000 Authentication-Results: pb1.pair.com smtp.mail=ekneuss@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ekneuss@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.160.42 as permitted sender) X-PHP-List-Original-Sender: ekneuss@gmail.com X-Host-Fingerprint: 209.85.160.42 mail-pb0-f42.google.com Received: from [209.85.160.42] ([209.85.160.42:35282] helo=mail-pb0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 1C/1A-35770-EFD788F4 for ; Fri, 13 Apr 2012 15:26:54 -0400 Received: by pbcun1 with SMTP id un1so4423420pbc.29 for ; Fri, 13 Apr 2012 12:26:51 -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; bh=KX8e6jStn+bJYIhQqMPVZg3a9ZGKCB05cRK5YBe/bHw=; b=R+kf5vSzTfchsvzi7AVznILWlJIavrSwiIm95EkL3OlSkTLJfgp/CIf+FB6t41V8pH snSDK35BWjd3abW5SXW68e1dCTqFUH9sFWVNGygp8jqxG48fQQjV25Ltk7OKC6sJXZ0S JFu/VBuxNjmX1W/zjU9h4siC6TCnCOPRcfnjhj8dBYetpWsLOyZ2f5vV3n3MWsyoEziU gyRlsAEQ/RrbsqAeVn8ibVHtib4WtXXhFfvl2gtGkHJHl/H3TUDpYgUcp/hkXjLTChYH ZGjXFTQKC2wDDXjvOeTQs7s1QqE1D0wvP1NWLWg+NGlxXn3Gg03PSvwvPWoImydpsn2M AEew== MIME-Version: 1.0 Received: by 10.68.221.35 with SMTP id qb3mr6935856pbc.136.1334345211166; Fri, 13 Apr 2012 12:26:51 -0700 (PDT) Sender: ekneuss@gmail.com Received: by 10.68.72.6 with HTTP; Fri, 13 Apr 2012 12:26:51 -0700 (PDT) In-Reply-To: <01ed01cd19aa$58810e80$09832b80$@alliantinternet.com> References: <01ed01cd19aa$58810e80$09832b80$@alliantinternet.com> Date: Fri, 13 Apr 2012 21:26:51 +0200 X-Google-Sender-Auth: dgPjI7X50WLRHuXYN3gf7vE3bfo Message-ID: To: Dmitri Snytkine Cc: PHP Internals Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] Ability to assign new object to a class property. From: colder@php.net (Etienne Kneuss) 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 = 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 > -- Etienne Kneuss http://www.colder.ch