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.
>
> unfortunately Im not a C Programmer myself else I've would have
> provided a patch for this =/
>
>
> 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 = Array('Hello World', 'Hello Me!');
list( , $greetMe) = 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 against.

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 = array();
    foreach ($object as $value){
        if (is_object($value)){
            $objectList[get_class($value)] = $value;
        }
    }
    return $objectList;
}

// 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


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

Reply via email to