You want to do WHAT with PHP? Chapter 5

3 Comments

Quick!  Raise your hand if you know the most underutilized feature in PHP?  If you're thinking type-juggling you're wrong (that's probably the most over-utilized feature).  It is, in my mind, SPL.  If you are doing any data processing whatsoever you are using arrays.  And most likely you are doing database queries, iterating over the results and doing your algorithm-ing.  But what if you have additional functionality that you need to have integrated with your data.  You could go the traditional route and copy and paste half your application around or you could build, what we like to call structured applications.  SPL allows you to do that.  How?  Well, that's one of the reasons why I wrote the book "You want to do WHAT with PHP?".  Here's your excerpt…

   Chapter 1: Networking and Sockets
   Chapter 2: Binary Protocols
   Chapter 3: Character Encoding
   Chapter 4: Streams
   Chapter 5: SPL
   Chapter 6: Asynchronous Operations with Some Encryption Thrown In
   Chapter 7: Structured File Access
   Chapter 8: Daemons
   Chapter 9: Debugging, Profiling, and Good Development
   Chapter 10: Preparing for Success

… snip

This code prints out

rewind()
valid()
current()
Found: string
next()
valid()
current()

Take a little time to look over the output and compare it to the code that we had written. There are a couple of things that you can glean from this output. First of all, you can see that the rewind() method is the first thing called. That tells you that foreach will always attempt to iterate over all items in this object. However, the fact that you have control over the execution of that code means that you can decide if you want to honor that or not. Generally you should, but if your object is a linked list that should not go back to the beginning, you can enforce that functionality in the object.

So far we’ve only looked echoing out the method names. So let’s take a look at a simple implementation of what this would look like.

class User
{
    public
$name;
}

class UserCollection implements Iterator
{
    private
$_users = array();
    private
$_position = 0;

    public function __construct()
   {
      $names
= array(
         'Bob'
,
         'David',
         'Steve',
         'Peter'
      );

      foreach ($names as $name) {
         $user = new User();
         $user->name = $name;
         $this->_users[] = $user;
      }
   }

   public function current()
   {
      return
$this->_users[$this->_position];
   }

   public function key()
   {
      return
$this->_position;
   }

   public function next()
   {
      $this
->_position++;
   }

   public function rewind()
   {
      $this
->_position = 0;
   }

   public function valid()
   {
      return isset
(
         $this
->_users[$this->_position]
      );
   }
}

$col = new UserCollection();
foreach
($col as $num => $user) {
   echo
"Found {$num}: {$user->name}n";
}

Figure 5.4

This code outputs

Found 0: Bob
Found 1: David
Found 2: Steve
Found 3: Peter

No surprises. But there’s a problem here. From a practical standpoint, it is not very beneficial because it doesn’t really do anything different from a regular array. So why would you use it? One reason is that this type of functionality is quite useful when you want to load things on demand. Lazy loading. Laziness is a programmer’s virtue, except when it comes to leaving the coffee pot empty. So what could this look like from a practical standpoint?

Let’s start with a database table.

3 thoughts on “You want to do WHAT with PHP? Chapter 5

  1. Seams a lot more work just to iterate over what is in effect an array?

    There are times when you need to adhere to an interface for managing multiple objects but just to go through all that to loop over them?

    Nah… you’re having a laugh.

    Whenever I see a simple example of use on posts I never see any benefit and the post ends up being completely pointless.

    Pull an example out of one of your applications or a website that is actually in use and I’m sure you’ll make your point even more concrete.

    Otherwise its a waste of time.

  2. Les, not everyone knows the SPL basics, which is why I put the basics in the book. But if I put the really cool examples in the excerpt, then nobody would buy the book. I don’t see what the problem is. This is an excerpt from the book, not an article in and of itself.

Leave a Reply

Your email address will not be published. Required fields are marked *