PHP
  Home arrow PHP arrow Page 2 - The Standard PHP Library, Part 2
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

The Standard PHP Library, Part 2
By: David Fells
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 17
    2004-12-14


    Table of Contents:
  • The Standard PHP Library, Part 2
  • The Array Object
  • Simple Array Iterators
  • Directory Iteration
  • Recursive Iteration

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    The Standard PHP Library, Part 2 - The Array Object
    ( Page 2 of 5 )

    Programmers familiar with Java and .NET will be at home with using the new ArrayObject class. The ArrayObject class has a definition like this:

    class ArrayObject implements IteratorAggregate, 
    ArrayAccess, Countable { public function __construct($array); public function append($value); public function count(); public function getArrayCopy(); public function getIterator(); public function offsetExists($index); public function offsetGet($index); public function offsetSet($index, $newval); public function offsetUnset($index); }

    As you see in the declaration, the ArrayObject class implements three interfaces. The Countable interface requires the public method count() to be defined and allows the built in PHP count() function to be used on the object. The ArrayAccess interface is used to override array access of objects. For example, the offsetGet($index) method is used in place of the normal array style: $array[$index]. This interfaces calls for the public methods offsetExists(), offsetGet(), offsetSet() and offsetUnset(). The IteratorAggregate interface requires the public method getIterator(), which allows the calling code to use an external iterator for object traversal.

    It is worth noting that the IteratorAggregate interface implements the Traversable abstract base interface, which is used to detect if a class is traversable using foreach. This interface must be implemented by either Iterator (an iterator object) or IteratorAggregate (an object to be iterated).

    Let's take a look at some sample code:

    $myArray = new ArrayObject();
    $myArray->append('a');
    $myArray->append('b');
    $myArray->append('c');
    
    echo 'First Element: '.$myArray->offsetGet(0).'<br />';
    echo 'Second Element: '.$myArray->offsetGet(1).'<br />';
    echo 'Third Element: '.$myArray->offsetGet(2).'<br />';
    echo 'Number of Elements: '.$myArray->count().'<br />';
    
    $myArray->offsetUnset(0);
    $myArray->offsetSet(1, 'a');
    echo 'First Element: '.$myArray->offsetGet(0).'<br />';
    echo 'Second Element: '.$myArray->offsetGet(1).'<br />';
    echo 'Third Element: '.$myArray->offsetGet(2).'<br />';
    echo 'Number of Elements: '.$myArray->count().'<br />';
    
    

    The first line creates the ArrayObject object and the next three lines store the values "a", "b" and "c'' in it. The next four lines demonstrate using the offsetGet() method and the count() method to retrieve values at a specified offset and to return the number of elements in the array, respectively. Next we unset the first index value by calling offsetUnset() and then change the value of the second index to "a". We then repeat the process of outputting the values in the array and the number of elements in the array. This script will output the following:

    First Element: a
    Second Element: b
    Third Element: c
    Number of Elements: 3
    
    First Element: 
    Second Element: a
    Third Element: c
    Number of Elements: 2
    

    Note that after calling unset, the index at position 0 still exists but has no value and is not included when calling count(). This seems like a counterintuitive behavior. It seems that the expected result would have been to completely remove the element - index and all - from the array and shift the other elements down. The term unset implies that after calling the method, the value is no longer set at all - meaning it is not set to NULL, 0, '' or anything else, and the index does not exist in the array. I would be interested to hear readers' thoughts on this, as I see it as poor implementation.



     
     
    >>> More PHP Articles          >>> More By David Fells
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT