Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:37146 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 85665 invoked from network); 19 Apr 2008 17:12:25 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Apr 2008 17:12:25 -0000 Authentication-Results: pb1.pair.com header.from=kalle@php.net; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=kalle@php.net; spf=unknown; sender-id=unknown Received-SPF: unknown (pb1.pair.com: domain php.net does not designate 67.19.4.138 as permitted sender) X-PHP-List-Original-Sender: kalle@php.net X-Host-Fingerprint: 67.19.4.138 8a.4.1343.static.theplanet.com Received: from [67.19.4.138] ([67.19.4.138:33140] helo=web.clantemplates.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 34/25-54185-8F72A084 for ; Sat, 19 Apr 2008 13:12:24 -0400 Received: from [127.0.0.1] (helo=localhost) by web.clantemplates.com with esmtpa (Exim 4.63) (envelope-from ) id 1JnGbs-000411-4z for internals@lists.php.net; Sat, 19 Apr 2008 10:11:57 -0700 Received: from 81.216.205.138 ([81.216.205.138]) by www.zesix.com (Horde MIME library) with HTTP; Sat, 19 Apr 2008 10:12:19 -0700 Message-ID: <20080419101219.g0zn2xghs0kkggko@www.zesix.com> Date: Sat, 19 Apr 2008 10:12:19 -0700 To: internals@lists.php.net References: <20080418104018.epguxp7v4844oowk@zesix.com> <1208543185.22418.9.camel@sbarrow-desktop> <9b3df6a50804182359g47ba818ci5bf78c59c66fc7f5@mail.gmail.com> In-Reply-To: <9b3df6a50804182359g47ba818ci5bf78c59c66fc7f5@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; DelSp="Yes"; format="flowed" Content-Disposition: inline Content-Transfer-Encoding: quoted-printable User-Agent: Internet Messaging Program (IMP) H3 (4.1.6) X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - web.clantemplates.com X-AntiAbuse: Original Domain - lists.php.net X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - php.net Subject: Re: [PHP-DEV] Array access on function returns From: kalle@php.net (Kalle Sommer Nielsen) 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 =3D Array(1337 =3D> 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. >> > >> > unfortunately Im not a C Programmer myself else I've would have >> > provided a patch for this =3D/ >> > >> > >> > Cheers >> > >> > Kalle Sommer Nielsen >> > Zend Certified Engineer >> > >> > >> >> >> -- >> PHP Internals - PHP Runtime Development Mailing List >> To unsubscribe, visit: http://www.php.net/unsub.php >> >> > You now, PHP has it's own way of implementing this - just use list() > > function ArrayTest(Array $range) { > return($range); > } > > $range =3D Array('Hello World', 'Hello Me!'); > list( , $greetMe) =3D ArrayTest($range); > echo $greet; I think it would be more suitable to have the array access syntax insted of the list(, , , $value) syntax, it helps readability for example. > > Although you example can't be implemented via list() because you key has > insane value, but usualy we are stuck with few keys with sensible values > like 0, 1, 2, 3. And you should realy return a string then, not an array. = I > never had a situation where list() couldn't help. And don't return a big > array because of one value - that will help saving memory :) That's agains= t. > > Well, I have an argument also for this one. > It could realy help then returning multidemensional arrays or if an array = of > objects is returned > > // $object is some object witch contains a lot of other objects in it's > properties > function getObjectList($object) { > $objectList =3D array(); > foreach ($object as $value){ > if (is_object($value)){ > $objectList[get_class($value)] =3D $value; > } > } > return $objectList; > } > > // Let's assume we know for sure that object with name we know exists > $myObject =3D new SomeCoolObject(); > $result =3D 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, f= or example on getimagesize(): if(!getimagesize($some_image_url)[2] =3D=3D IMAGETYPE_PNG) { =09die('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