php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #32413 Array accessed method
Submitted: 2005-03-22 17:34 UTC Modified: 2014-12-31 16:10 UTC
From: barnaby dot scott at uk dot wanadoo dot com Assigned:
Status: Wont fix Package: *General Issues
PHP Version: Irrelevant OS: N/A
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-12-31 16:10 UTC] nikic@php.net
-Status: Open +Status: Wont fix -Package: Feature/Change Request +Package: *General Issues
 [2014-12-31 16:10 UTC] nikic@php.net
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();
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 19:01:29 2024 UTC