On Saturday 19 April 2008, Kalle Sommer Nielsen wrote:
> Hi Arvids
>
> Quoting Arvids Godjuks <[EMAIL PROTECTED]>:
> > 2008/4/18, Sam Barrow <[EMAIL PROTECTED]>:
> >> 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
[EMAIL PROTECTED]               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

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to