Zend Framework Webcast

02 Dec 2005

I just finished listening to the Zend Framework Webcast, hosted by php|architect. The recording will be available soon, and I'll update this post to provide a link as soon as it is.

The core focus of the framework is Extreme Simplicity. In order to achieve this, it must be very easy to use. This doesn't just mean that it must be simple to write code - it also needs to be useful in existing PHP environments without the need for external libraries or custom modifications. In many cases, this means that features must be implemented in pure PHP, so that they're available everywhere. For example, the search component is a pure PHP implementation of Lucene, and the front controller doesn't rely on a sophisticated collection of mod_rewrite rules.

Another goal of the framework is to allow developers to use only what they need. If you just want to use the framework's nice search component, you can. You can pick and choose from among the framework's features without having to choose its way of doing everything.

However, this is a real framework and not just a collection of components. The framework will provide components that can be used separately, but it also provides the glue to bring them all together. In addition, the framework will be distributed in its entirety, so there's no need to worry about installing invidual components or resolving dependencies.

The following figure provides an overview of the framework (click on the image for a larger view):

Three components were demonstrated with use cases - ZActiveRecord, ZMail, and ZSearch.

ZActiveRecord, which is an implementation of Martin Fowler's Active Record pattern, provides a nice data interface. Results are returned as a ZActiveRecordCollection object, which implements the Iterator and ArrayAccess interfaces, so result sets behave like ordinary PHP arrays:

  1.  
  2. class Person extends ZActiveRecord {}    
  3.  
  4. $people = Person::findAll(array('nameFirst' => 'Daniel'));
  5.  
  6. foreach ($people as $person)
  7. {
  8.     echo $person->nameFirst . "\n";
  9. }
  10.  

For small queries, there are various find() methods provided, such as findFirst():

  1.  
  2. class Person extends ZActiveRecord {}
  3.  
  4. $zeev = Person::findFirst(array('nameFirst' => 'Zeev'),
  5.                                 'nameLast'  => 'Suraski'));
  6.  

For more complicated queries, you can use findBySql(), which allows you to provide your own SQL query. This is better than having a complicated query builder.

ZMail was demonstrated with a simple example (I've modified the email addresses out of courtesy):

  1.  
  2. $mail = new ZMail();
  3.  
  4. $mail->setFrom('daniel@example.org', 'Daniel Kushner');
  5. $mail->addTo('paul@example.org', 'Paul M. Jones');
  6. $mail->addTo('matthew@example.org', 'Matthew Weier O\'Phinney');
  7.  
  8. $mail->setSubject('ZMail Demo');
  9. $mail->setBodyText('Hi All, here is some text.');
  10. $mail->setBodyHtml('<blink>HTML</blink>');
  11.  
  12. $mail->send();
  13.  

ZSearch is arguably one of the coolest features of the Zend Framework. It provides both a simple API and a more sophisticated query builder. For simple queries, just use the find() method:

  1.  
  2. $index = ZSearch::open('/tmp/index');
  3.  
  4. $hits = $index->find('zend');
  5.  
  6. foreach ($hits as $hit)
  7. {
  8.     echo $hit->getDocument()->getFieldValue('title') . "\n";
  9. }
  10.  

The find() method also supports Google-like queries:

  1.  
  2. $index = ZSearch::open('/tmp/index');
  3.  
  4. $hits = $index->find('zend php -java');
  5.  
  6. foreach ($hits as $hit)
  7. {
  8.     echo $hit->getDocument()->getFieldValue('title') . "\n";
  9. }
  10.  

As you can see, most of the objects implement the Iterator and ArrayAccess interfaces, so working with them is very easy. The idea is that the framework takes care of all of the complexity, so that the code you write is extremely simple. In a way, the Zend Framework makes PHP development as easy as SimpleXML makes XML.