This entry was posted on donderdag, september 6th, 2007 at 22:33 and is filed under Algemeen. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Site Search:
06/09/2007
Hello
I’m mostly developing with Zend Framework. Now I came to something that is annoying to me (and maybe other developers will say this). Let’s say we do the next thing.
$registry = new Zend_Registry();
Zend_Registry::setInstance( $registry );
//that is common code above, now we gonna register an array
$registry->set( 'myArray' , array(1,2,3,4,5,6,7,8,9) );
Now I’m gonna call the first element of myArray in one of my controllers because I need it for some reason.
$myArray = Zend_Registry::get('myArray');
$one = $myArray[0];
I think this can be much easier and more like this way:
$one = Zend_Registery::get('myArray')[0];
or
$registry = Zend_Registry::getInstance();
$one = $registry->get('myArray')[0];
$two = $registry->get('myArray')[1];
An other example without use of Zend Framework:
function returnArray()
{
return array(1,2,3,4,5);
}
$firstElement = returnArray()[0];
$lastElement = returnArray()[4];
Maybe this can get some issue’s for large functions. But you use this in the other way with objects. Watch the example:
$array = array( $obj1 , $obj2 , $obj3 );
echo $array[0]->functionObj1();
So if an function returns an array with objects, we could have next:
function returnArray()
{
return array( $obj1 , $obj2 );
}
echo returnArray()[0]->functionObj1();
So dev team of PHP, could you discuss this idea and maybe you can add this to PHP6.
Contact address: stijnleenknegt@gmail.com
Greetings
read comments (18)
september 7th, 2007 at 13:11
learn some english, then make demands
september 7th, 2007 at 20:05
Stijn Leenknegt’s Blog: [PHP6] function-return-array idea!…
…
september 7th, 2007 at 21:30
My english is bad, but if you are real coders you should understand this “problem” only by reading the code!
september 7th, 2007 at 22:44
[…] Leenknegt has posted a suggestion of his directed towards the developers behind PHP6 for a different way to return array values that […]
september 8th, 2007 at 01:59
This is a really good idea. Let’s hope the PHP 6 dev team gets wind of it and decides to include it.
september 8th, 2007 at 02:42
@TheGuy
If all english speakers are such smart guys, the world would be a great place to live it. Besides, all the great web programming languages where created by NON-ENGLISH speakers. So my suggestion to you is to go grab a bear in one hand and a tv remote in the other and find a sofa to spend your rest of your life on.
@Stijn
Sorry I had to reply that “Guy”… Nice suggestion, I will try adding it to the php parser. I will try to send you the C++ code if you want so you can post it as part of the suggestion to the PHP team.
september 8th, 2007 at 06:59
This is done in other languages (Python being one of them) and it’s great. I’d love to see that in PHP.
september 8th, 2007 at 10:36
This is a well known problem of Zend Engine, and this syntax would definitely help a lot.
september 8th, 2007 at 12:02
No. Because:
You should not return data structures as arrays! Returning arrays is only allowed in terms of “clean” programming, if the array is a list of similiar elements.
If you want to return a data structure, then return it as an object:
echo returnObject()->someProperty->functionObj1();
If you’re using an opcode cache, then there’s nearly no performance loss in using objects instead of arrays.
september 8th, 2007 at 12:37
@dmondark: you may try that
would be nice, less work for the php dev team :p
@Thomas Koch: it’s not only for classes, but also for functions.
september 8th, 2007 at 14:45
@Thomas Koch: That’s a fairly Object Obsessive Programming point of view. I understand the advantages of returning an object when you need the structure, but given PHP’s history as a procedural, loosely-typed language, I see no problem with providing a syntax for returning arrays. There’s nothing to prevent you from returning a single object if you want to.
september 8th, 2007 at 21:29
Excellent idea! This can be done in javascript, why not PHP?
september 10th, 2007 at 09:34
I dont know if PHP’s internal implementation of arrays shares something with the implementation of the objects implementing the ArrayAccess interface but if they ever decide to make your idea possible I want to also be able to do:
$myObject->method()[1];
where method() returns an object implementing the ArrayAccess interface.
september 10th, 2007 at 16:28
Great idea. But to get more exposure from the PHP dev team. It’s probably better to post it to the internals list as a suggestion for the upcomming 5.3.
september 10th, 2007 at 16:48
I have often been annoyed by the need to split into two statements. It would be well in line with the chaining that ZF for instance is supporting:
$object->getOtherObject()->getProperty();
One problem is obviously what would happen if the function fails for some reason. Writing it compact like this makes it impossible to verify the return value before continuing.
I think it will be difficult to introduce this feature without better support for declaring return types of functions.
september 29th, 2007 at 15:41
I think this is a great idea for PHP6, it would sure make life easier.
oktober 12th, 2007 at 00:26
I add something to my blog article. Javascript use this to. Next example shows it.
document.getElementsByTagName(’input’)[1].getAttribute(’value’);
Is javascript owning php? I hope not :p
november 21st, 2007 at 13:38
Insanity: a perfectly rational adjustment to the insane world.