|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2005-03-22 17:34 UTC] barnaby dot scott at uk dot wanadoo dot com
Description:
------------
I was just playing about with the array functions, and I feel that there is a missing behaviour. It would be a sort of shortcut to a for loop, the way that array_walk is a shortcut to looping through the array and performing an action on each element. My proposed shortcut function would replace this sort of code:
for ( $i=0; $i < count( $array ); $i++ ) {
$tmp = $array[ $i ];
$tmp->someFunc();
}
With something like this:
$array->someFunc();
Essentially treating the array as an object, obviously all of the elements in the array would have to implement the someFunc method, but then that could be taken care of with some type hinting. It's really just something that I thought would make my code a lot cleaner.
Cheers
Barney
Reproduce code:
---------------
(The sort of syntax that i was thinking of)
interface ExampleInt {
public function printVar();
}
class Example implements ExampleInt {
private $aVar;
public function __construct( $val ) {
$this->aVar = $val;
}
public function printVar() {
print $this->aVar . '<br>';
}
}
$exArray = ExampleInt array();
$exArray[] = new Example( 'one' );
$exArray[] = new Example( 'two' );
$exArray[] = new Example( 'three' );
$exArray[] = new Example( 'four' );
$exArray[] = new Example( 'five' );
$exArray->printVar();
Expected result:
----------------
one
two
three
four
five
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Dec 14 14:00:02 2025 UTC |
This won't be implemented as it's way too much magic. And doing it explicitly is a one-liner: foreach ($array as $obj) $obj->someFunc();