Req #32413 [Opn->Wfx]: Array accessed method
| From: | nikic@php.net | Date: | Wed, 31 Dec 2014 16:10:08 +0000 |
| Subject: | Req #32413 [Opn->Wfx]: Array accessed method | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-189562@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=32413&edit=1
ID: 32413
Updated by: nikic@php.net
Reported by: barnaby dot scott at uk dot wanadoo dot com
Summary: Array accessed method
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
-Package: Feature/Change Request
+Package: *General Issues
Operating System: N/A
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
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();
Previous Comments:
------------------------------------------------------------------------
[2005-03-22 17:34:32] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=32413&edit=1