Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:37147 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 26821 invoked from network); 19 Apr 2008 21:52:06 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Apr 2008 21:52:06 -0000 Authentication-Results: pb1.pair.com smtp.mail=larry@garfieldtech.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=larry@garfieldtech.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain garfieldtech.com from 76.96.62.64 cause and error) X-PHP-List-Original-Sender: larry@garfieldtech.com X-Host-Fingerprint: 76.96.62.64 qmta07.westchester.pa.mail.comcast.net Received: from [76.96.62.64] ([76.96.62.64:40349] helo=QMTA07.westchester.pa.mail.comcast.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 58/DC-54185-4896A084 for ; Sat, 19 Apr 2008 17:52:05 -0400 Received: from OMTA13.westchester.pa.mail.comcast.net ([76.96.62.52]) by QMTA07.westchester.pa.mail.comcast.net with comcast id FU7D1Z00417dt5G570J800; Sat, 19 Apr 2008 21:50:30 +0000 Received: from earth.ufp ([24.13.255.226]) by OMTA13.westchester.pa.mail.comcast.net with comcast id FZs21Z0014trKQ83Z00000; Sat, 19 Apr 2008 21:52:02 +0000 X-Authority-Analysis: v=1.0 c=1 a=CrgN8fJF7ooA:10 a=xFA32zNfei8A:10 a=yoKzjkRikO1usHryVjcA:9 a=bd1nK43HarIRMvGnFb8A:7 a=Y8Uqpew7VS3vluG7q9U2p6eyixAA:4 a=MSl-tDqOz04A:10 a=Wo-382_gzx0A:10 a=FHBbIDN7CdwA:10 a=LY0hPdMaydYA:10 Received: from localhost (localhost [127.0.0.1]) by earth.ufp (Postfix) with ESMTP id 3CED7D8523 for ; Sat, 19 Apr 2008 16:52:01 -0500 (CDT) Received: from earth.ufp ([127.0.0.1]) by localhost (earth.ufp [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id pmX7e7GYNoTA for ; Sat, 19 Apr 2008 16:51:59 -0500 (CDT) Received: from vulcan (unknown [192.168.42.1]) by earth.ufp (Postfix) with ESMTP id DFD2AD7AA6 for ; Sat, 19 Apr 2008 16:51:58 -0500 (CDT) To: internals@lists.php.net Date: Sat, 19 Apr 2008 16:51:54 -0500 User-Agent: KMail/1.9.6 (enterprise 0.20070907.709405) References: <20080418104018.epguxp7v4844oowk@zesix.com> <9b3df6a50804182359g47ba818ci5bf78c59c66fc7f5@mail.gmail.com> <20080419101219.g0zn2xghs0kkggko@www.zesix.com> In-Reply-To: <20080419101219.g0zn2xghs0kkggko@www.zesix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-ID: <200804191651.55376.larry@garfieldtech.com> Subject: Re: [PHP-DEV] Array access on function returns From: larry@garfieldtech.com (Larry Garfield) On Saturday 19 April 2008, Kalle Sommer Nielsen wrote: > Hi Arvids > > Quoting Arvids Godjuks : > > 2008/4/18, Sam Barrow : > >> On Fri, 2008-04-18 at 10:40 -0700, Kalle Sommer Nielsen wrote: > >> > Hey Internals > >> > > >> > I've been wondering for quite some time why PHP doesn't allow you to > >> > >> access > >> > >> > arrays when you assign it to a value like in Javascript: > >> > > >> > function ArrayTest(Array $range) > >> > { > >> > return($range); > >> > } > >> > > >> > $range = Array(1337 => Array('Hello World')); > >> > > >> > echo ArrayTest($range)[1337}; > >> > >> I was just about to write an email asking this same exact question this > >> afternoon. This could be very useful, I don't see any reason not to have > >> it. > >> > >> > I would really like to see this introduced in PHP sometime (Perhaps as > >> > an extra addition to 5.3's new and sleek features). > >> > > >> > This is very useful in a few areas and can save a few lines of code > >> > here and there. > > // Let's assume we know for sure that object with name we know exists > > $myObject = new SomeCoolObject(); > > $result = getObjectList($myObject)['db']->query($sql)->execute(); > > > > Well, a stupid one example. So to me it is a little pointless, because > > you just can't check if key exists and that will be a warning. So it is > > very disputeable if we need it. > > I think the usage of this would be in cases where you know the key exists, > for example on getimagesize(): > > if(!getimagesize($some_image_url)[2] == IMAGETYPE_PNG) > { > die('Please supply a valid url to a png image...'); > } > > Theres quite alot functions where you need to only get one key and insted > of doing the list() (as written above) it would be nicer with syntax. > > > Kalle Sommer Nielsen > Zend Certified Engineer It is also useful if returning an object that implements ArrayAccess and lazy-loads data. $name = load_something($id)['name']; If using lazy-loading internally, that could offer very cheap access to the name property of the "something" object, with only a tiny bit of code. And it's really no more error prone than: $something = load($id); $name = $something['name']; Presumably you're doing some sort of automated error handling either way (trigger, exceptions, whatever.) -- Larry Garfield AIM: LOLG42 larry@garfieldtech.com ICQ: 6817012 "If nature has made any one thing less susceptible than all others of exclusive property, it is the action of the thinking power called an idea, which an individual may exclusively possess as long as he keeps it to himself; but the moment it is divulged, it forces itself into the possession of every one, and the receiver cannot dispossess himself of it." -- Thomas Jefferson