PHP: Standard PHP Library (SPL) 関数 - Manual
PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links | my php.net 
search for in the  
<socket_writeArrayIterator::current>
view the version of this page
Last updated: Tue, 21 Dec 2004

CXIV. Standard PHP Library (SPL) 関数

導入

SPLは、標準的な問題を解決するためのインターフェイスやクラスを集めたものです。

インストール手順

To be written.

定義済みの定数

これらの定数は、この拡張モジュールで定義されており、 この拡張モジュールがPHP内部にコンパイルされているか実行時に動的にロー ドされるかのどちらかの場合のみ使用可能です。

RIT_LEAVES_ONLY (integer)

RIT_SELF_FIRST (integer)

RIT_CHILD_FIRST (integer)

CIT_CALL_TOSTRING (integer)

CIT_CATCH_GET_CHILD (integer)

目次
ArrayIterator::current --  Return current array entry
ArrayIterator::key --  Return current array key
ArrayIterator::next --  Move to next entry
ArrayIterator::rewind --  Rewind array back to the start
ArrayIterator::seek --  Seek to position
ArrayIterator::valid --  Check whether array contains more entries
ArrayObject::append --  Appends the value
ArrayObject::__construct --  Construct a new array object
ArrayObject::count --  Return the number of elements in the Iterator
ArrayObject::getIterator --  Create a new iterator from an ArrayObject instance
ArrayObject::offsetExists --  Returns whether the requested $index exists
ArrayObject::offsetGet --  Returns the value at the specified $index
ArrayObject::offsetSet --  Sets the value at the specified $index to $newval
ArrayObject::offsetUnset --  Unsets the value at the specified $index
CachingIterator::hasNext --  Cehck whether the inner iterator has a valid next element
CachingIterator::next --  Move the iterator forward
CachingIterator::rewind --  Rewind the iterator
CachingIterator::__toString --  Retrun the string representation of the current element
CachingIterator::valid --  Check whether the current element is valid
CachingRecursiveIterator::getChildren --  Return the inenr iteraor's children as a CachingRecursiveIterator
CachingRecursiveIterator::hasChildren --  Check whether the current element of the inner iterator has children
DirectoryIterator::__construct --  Constructs a new dir iterator from a path
DirectoryIterator::current --  Return this (needed for Iterator interface)
DirectoryIterator::getATime --  Get last access time of file
DirectoryIterator::getCTime --  Get inode modification time of file
DirectoryIterator::getChildren --  Returns an iterator for the current entry if it is a directory
DirectoryIterator::getFilename --  Return filename of current dir entry
DirectoryIterator::getGroup --  Get file group
DirectoryIterator::getInode --  Get file inode
DirectoryIterator::getMTime --  Get last modification time of file
DirectoryIterator::getOwner --  Get file owner
DirectoryIterator::getPath --  Return directory path
DirectoryIterator::getPathname --  Return path and filename of current dir entry
DirectoryIterator::getPerms --  Get file permissions
DirectoryIterator::getSize --  Get file size
DirectoryIterator::getType --  Get file type
DirectoryIterator::isDir --  Returns true if file is directory
DirectoryIterator::isDot --  Returns true if current entry is '.' or '..'
DirectoryIterator::isExecutable --  Returns true if file is executable
DirectoryIterator::isFile --  Returns true if file is a regular file
DirectoryIterator::isLink --  Returns true if file is symbolic link
DirectoryIterator::isReadable --  Returns true if file can be read
DirectoryIterator::isWritable --  Returns true if file can be written
DirectoryIterator::key --  Return current dir entry
DirectoryIterator::next --  Move to next entry
DirectoryIterator::rewind --  Rewind dir back to the start
DirectoryIterator::valid --  Check whether dir contains more entries
FilterIterator::current --  Get the current element value
FilterIterator::getInnerIterator --  Get the inner iterator
FilterIterator::key --  Get the current key
FilterIterator::next --  Move the iterator forward
FilterIterator::rewind --  Rewind the iterator
FilterIterator::valid --  Check whether the current element is valid
LimitIterator::getPosition --  Return the current position
LimitIterator::next --  Move the iterator forward
LimitIterator::rewind --  Rewind the iterator to the specified starting offset
LimitIterator::seek --  Seek to the given position
LimitIterator::valid --  Check whether the current element is valid
ParentIterator::getChildren --  Return the inner iterator's children contained in a ParentIterator
ParentIterator::hasChildren --  Check whether the inner iterator's current element has children
ParentIterator::next --  Move the iterator forward
ParentIterator::rewind --  Rewind the iterator
RecursiveDirectoryIterator::getChildren --  Returns an iterator for the current entry if it is a directory
RecursiveDirectoryIterator::hasChildren --  Returns whether current entry is a directory and not '.' or '..'
RecursiveDirectoryIterator::key --  Return path and filename of current dir entry
RecursiveDirectoryIterator::next --  Move to next entry
RecursiveDirectoryIterator::rewind --  Rewind dir back to the start
RecursiveIteratorIterator::current --  Access the current element value
RecursiveIteratorIterator::getDepth --  Get the current depth of the recursive iteration
RecursiveIteratorIterator::getSubIterator --  The current active sub iterator
RecursiveIteratorIterator::key --  Access the current key
RecursiveIteratorIterator::next --  Move forward to the next element
RecursiveIteratorIterator::rewind --  Rewind the iterator to the first element of the top level inner iterator
RecursiveIteratorIterator::valid --  Check whether the current position is valid
SimpleXMLIterator::current --  Return current SimpleXML entry
SimpleXMLIterator::getChildren --  Returns an iterator for the current entry if it is a SimpleXML object
SimpleXMLIterator::hasChildren --  Returns whether current entry is a SimpleXML object
SimpleXMLIterator::key --  Return current SimpleXML key
SimpleXMLIterator::next --  Move to next entry
SimpleXMLIterator::rewind --  Rewind SimpleXML back to the start
SimpleXMLIterator::valid --  Check whether SimpleXML contains more entries
class_implements --  Return the interfaces which are implemented by the given class
class_parents --  Return the parent classes of the given class
iterator_count --  Count the elements in an iterator
iterator-to-array --  Copy the iterator into an array
spl_classes --  Return available SPL classes


add a note add a note User Contributed Notes
Standard PHP Library (SPL) 関数
<nospam>mike[ at ]emesdee.net</nospam>
22-Nov-2004 08:47
Excelent article here by Harry Fuecks...



and some auto generated documentation that could be of some use here...

mastabog at hotmail dot com
17-Aug-2004 09:41
Marcus Boerger has done a wonderful job developing the SPL. He also provided many examples using the SPL that can be found in the php5 sources. Just unpack the sources and in the ext/spl/examples directory you have some very nice ones. Thank you Marcus for all your efforts!

Now, a contribution of mine (i think it will be implemented later anyway). The RecursiveIteratorIterator could use a depth limit option. Very useful in many situations (e.g. show just the 1st subdirectory of a list of dirs). I'm sure this can be done in other ways. Here's my 2 cents:

<?php
/**
 * Limit Depth RecursiveIteratorIterator class
 *
 */
class LimitRecursiveIteratorIterator extends RecursiveIteratorIterator
{
  
protected $depth_limit;

  
/**
     * No depth limit by default
     *
   **/
  
public function __construct (Iterator $it, $mode = RIT_SELF_FIRST, $depth_limit = -1)
   {
      
parent::__construct($it, $mode);
      
$this->depth_limit = $depth_limit;
   }

  
/**
     * After the call to next() if depth is bigger than limit then
     * just skip all subIterators for that depth until depth end.
     *
   **/
  
public function next ()
   {
      
parent::next();

       if (
$this->getDepth() == $this->depth_limit)
       {
           while (
$this->getSubIterator()->valid())
              
$this->getSubIterator()->next();
          
parent::next();
       }
   }
}
?>

Then you can try this:

<?php
/**
 * Directories only filter iterator class
 *
 */
class DirectoriesOnlyIterator extends FilterIterator implements RecursiveIterator
{
  
public function __construct ($path)
   {
      
parent::__construct(new RecursiveDirectoryIterator($path));
   }

  
public function accept()
   {
       return
$this->getInnerIterator()->hasChildren();
   }

  
public function hasChildren ()
   {
       return
$this->getInnerIterator()->hasChildren();
   }
  
  
public function getChildren ()
   {
       return new
self($this->getInnerIterator()->getPathname());
   }
}

$it = new LimitRecursiveIteratorIterator(new DirectoriesOnlyIterator('c:'), RIT_SELF_FIRST, 2);

// list all dirs and 1st subdir of the c: drive (might take a while depending on how many you have)
foreach ($it as $key => $value)
{
   echo
str_repeat('    ', $it->getDepth()) . "$value\n";
}

?>

This is considerably faster than using just the RecursiveIteratorIterator and ignoring yourself in the foreach loop the values for depth > limit (i.e. if($it->getDepth() > $limit) continue;). that is because the class will still parse everything up to the last depth level of every head node.

You can then play and display nice trees (might need a while() loop or the CachingRecursiveIterator to detect end nodes/leafs). There is already an example provided by Marcus in the ext/spl/examples dir i mentioned above.

Happy SPL-ing :),
Bogdan

P.S. I think some of the classes should call rewind() at instantiation time ... If you try to put a Caching* object in a foreach loop you will lose the first/last element. Instead, you should call rewind() and then go with a while($cit->valid()) loop and using current() and key() inside it.
dave at tunasoft dot com
10-Aug-2004 01:28
There are some interfaces used here that are not documented.  It took a bit to figure this one out, but you can create your own ArrayObject type class (that is, one who's objects can be access using the array [$index] syntax).

Your class must just implement ArrayAccess.  Which has four abstract methods you must define.  For example:

<?php
  
class Collection Implements ArrayAccess{
  
      
protected $array;
      
       function
offsetExists($offset){
          
           if(isset(
$this->array[$offset])){
               return
TRUE;
           }
           else{
               return
FALSE;
           }
          
       }
      
       function
offsetGet($offset){
          
           return
$this->array[$offset];
          
       }
      
       function
offsetSet($offset, $value){
          
           if(
$offset){
              
$this->array[$offset] = $value;
           }
           else{
              
$this->array[] = $value;
           }
      
       }
      
       function
offsetUnset($offset){
      
       }
  
  
   }
?>

You'll have to jump through a couple more hoops to get foreach and print_r and the likes to behave properly.  But with just this, you can :

<?php
     $col
= new Collction();
    
$col[] = new ObjectX();
    
$col[] = new ObjectX(123);

     echo
$col[0]->name;
    
// etc.
?>
phil &ampersat; flatnet.net
17-Apr-2004 10:11
Here's a sample implementation of the RecursiveDirectoryIterator class. It prints a simple treeview of a given directory:
<?php
function recurse($it) {
   echo
'<ul>';
   for( ;
$it->valid(); $it->next()) {
       if(
$it->isDir() && !$it->isDot()) {
          
printf('<li class="dir">%s</li>', $it->current());
           if(
$it->hasChildren()) {
              
$bleh = $it->getChildren();
               echo
'<ul>' . recurse($bleh) . '</ul>';
           }
       } elseif(
$it->isFile()) {
           echo
'<li class="file">'. $it->current() . ' (' . $it->getSize(). ' Bytes)</li>';
       }
   }
   echo
'</ul>';
}

recurse(new RecursiveDirectoryIterator('D:/'));
?>

<socket_writeArrayIterator::current>
 Last updated: Tue, 21 Dec 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: /
Last updated: Mon Mar 14 08:13:06 2005 Local time zone must be set--see zic manual page