php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #27929 SPL: change visibility of ArrayIterator::__construct
Submitted: 2004-04-09 07:02 UTC Modified: 2004-08-30 20:32 UTC
From: juwe at clasennet dot de Assigned: helly
Status: Closed Package: Feature/Change Request
PHP Version: 5.0.0RC2 OS: *
Private report: No CVE-ID:
 [2004-04-09 07:02 UTC] juwe at clasennet dot de
Description:
------------
Hello,

when I just tested the SPL I found a necessity to create an iterator for an existing array. Since ArrayIterator::__construct() is currently declared private, I've first had to create a completely unnecessary (well, at least from my point of view) instance of ArrayObject, just to produce an Iterator with ArrayObject::getInstance().

Could someone please have a look at that one and decide if it is possible to allow public access on ArrayIterators without the necessety to create foreign objects in before?

I know this is just a minor issue at the moment, but I'd prefer such things beeing changed, before they are used in a productive environment.

Thanks 
Jens Clasen

P.S.: Please excuse my bad english - I am no native speaker.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-04-09 08:18 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

ArrayObject encapsulates the array and ArrayIterator depends on that encapsulation. For the moment i do not
see any reason to change that behavior.
 [2004-04-09 09:05 UTC] juwe at clasennet dot de
First of all, thanks for Your kind reply.
If I could use ArrayObject for anything else than producing an iterator, I'd agree, that there is indeed no necessity for any further possibility to create an iterator for an array. In this case I'd simply use ArrayObject instead of Arrays and I'd be done with that one. 

But as it is now, I do have to convert my array into an object just for looping through it outside of my class.

Let me present an example for this:

<?php
  class MyClass implements IteratorAggregate
  {
    private $myData;
    function __construct()
    {
      $this->myData=array();
    }
    public function addValue($value)
    {
      /* do something with value */
      $this->myData[]=$value*2;
    }
    public function getIterator()
    {
      /* works:*/
      $tmpObject=new ArrayObject($this->myData);
      return $tmpObject->getIterator();
      /* fails:
       return (new ArrayObject($this->myData))->getIterator();
       return new ArrayIterator($this->myData);
       */
    }
  }

I mean, it is no problem to create one new temporary var vor the ArrayObject, but in the end I have the feeling, that its not, well maybe "perfect" is the right term, but a bloody workaround.

At the moment I do see four possible solutions:

1) Allow creating own instances from ArrayIterator

2) Add a static method to ArrayObject to create array-Iterators directly for any array supplied.

3) Add much more functionality to ArrayObject. (At least adding, accessing and removing elements would be fine.)

4) Make ArrayObject usable with all the existing array functions (e.g. sort(), etc.).

Yours Jens
 [2004-04-23 20:21 UTC] helly@php.net
Here's a patch to fix the open SPL issues:
http://marcus-boerger.de/php/ext/spl/spl-20040422.diff.txt
It would be nice to have it tested prior to comitting it after RC2.

 [2004-08-30 20:32 UTC] helly@php.net
Thank you for your bug report. This issue has already been fixed
in the latest released version of PHP, which you can download at 
http://www.php.net/downloads.php


 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Sun Apr 20 19:01:51 2014 UTC