About the Author

Chris Shiflett

Hi, I’m Chris: entrepreneur, community leader, husband, and father. I live and work in Boulder, CO.


Zend Framework Webcast

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:

<?php 

class Person extends ZActiveRecord {}    

$people Person::findAll(array('nameFirst' => 'Daniel'));

foreach (
$people as $person)
{
    echo 
$person->nameFirst "\n";
}

?>

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

<?php 

class Person extends ZActiveRecord {}

$zeev Person::findFirst(array('nameFirst' => 'Zeev'),
                                
'nameLast'  => 'Suraski'));

?>

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):

<?php 

$mail 
= new ZMail();

$mail->setFrom('daniel@example.org''Daniel Kushner');
$mail->addTo('paul@example.org''Paul M. Jones');
$mail->addTo('matthew@example.org''Matthew Weier O\'Phinney');

$mail->setSubject('ZMail Demo');
$mail->setBodyText('Hi All, here is some text.');
$mail->setBodyHtml('<blink>HTML</blink>');

$mail->send();

?>

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:

<?php 

$index 
ZSearch::open('/tmp/index');

$hits $index->find('zend');

foreach (
$hits as $hit)
{
    echo 
$hit->getDocument()->getFieldValue('title') . "\n";
}

?>

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

<?php 

$index 
ZSearch::open('/tmp/index');

$hits $index->find('zend php -java');

foreach (
$hits as $hit)
{
    echo 
$hit->getDocument()->getFieldValue('title') . "\n";
}

?>

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.

About this post

Zend Framework Webcast was posted on Fri, 02 Dec 2005. If you liked it, follow me on Twitter or share:

30 comments

1.Chris Shiflett said:

Chris Cornutt also has a nice summary:

http://blog.phpdeveloper.org/?p=23

Fri, 02 Dec 2005 at 20:59:42 GMT Link


2.starmonkey said:

sounds like php on rails!

== phails? :)

Fri, 02 Dec 2005 at 21:19:57 GMT Link


3.Jacques Marneweck said:

Interesting. Did Zend mention anything about them using that framework on the Zend.com website?

Fri, 02 Dec 2005 at 22:59:25 GMT Link


4.Chris Shiflett said:

Yes, Andi mentioned that Zend would be using the framework for their own web site.

Fri, 02 Dec 2005 at 23:27:10 GMT Link


5.Deuce said:

What about the templating? Any word on what it might resemble? I really like savant, but I'm curious how that will work out.

Sat, 03 Dec 2005 at 04:19:48 GMT Link


6.H.S.Sekhon said:

Chris, good work on the summary. I can't wait to see this f/w roll out for our use. Just one thing though, how come logging and stats have been made a part of the front-controller through a plug-in subset? Any special reason for this? Isn't this supposed to be a part of the business logic since it interfaces with some kind of data repository?

Sat, 03 Dec 2005 at 04:57:41 GMT Link


7.Chris Cornutt said:

Deuce, if I remember right, they said that by default it would incorporate Smarty, but will be extendible for just about any kind of templating class you feel like integrating...

Sat, 03 Dec 2005 at 11:59:20 GMT Link


8.Jared White said:

The Zend Framework sounds great. Looking forward to it.

This may sound like a wacky question, but I am DYING of curiosity -- how does the findAll static method on Person work? Because the static method would be defined in ZActiveRecord, not Person, and therefore theoretically it would create new ZActiveRecord objects. But obviously it needs to create Person objects, so how does the static method know it's being called as a subclass? I spent hours trying to figure out how to get the class name of the subclass when the static method was in the superclass, and I gave up. :(

Thanks for any light you can shed on this.

Jared

Sat, 03 Dec 2005 at 19:09:28 GMT Link


9.Jake Grimley said:

I'd like to second Jared's question...

For my money this was the only thing preventing you from implementing the ActiveRecord pattern in PHP, (even v5).

Sun, 04 Dec 2005 at 17:09:16 GMT Link


10.Chris Shiflett said:

The recording is available here:

http://www.phparch.com/webcasts/recordings/andi.php

Mon, 05 Dec 2005 at 17:44:19 GMT Link


11.Chris Shiflett said:

Hi Jared,

I'm not quite sure what you're asking, but all you'd need to do is define findAll() as a public static method:

<?php

class ZActiveRecord

{

static public function findAll()

{

echo "findAll()\n";

}

}

class Person extends ZActiveRecord {}

Person::findAll();

?>

(Sorry for the formatting - I need to allow pre-formatted markup.)

Mon, 05 Dec 2005 at 18:09:23 GMT Link


12.Chris Shiflett said:

I should also point out that you're not instantiating an object in this case, so perhaps that clarifies something.

Mon, 05 Dec 2005 at 18:13:05 GMT Link


13.Jake Grimley said:

Okay, this is academic as there is obviously some way to do it, but what Jared is getting at is this:

<?PHP

class ZActiveRecord

{

static public function findAll()

{

echo __class__;

}

}

class Person extends ZActiveRecord {}

Person::findAll();

?>

Run this code, and it will print 'ZActiveRecord' and not 'Person'. This is by design in PHP but some people consider it a flaw in the object model.

Jared's point is, how do Zend get ZActiveRecord to know what object to instantiate, as when the subclass looks up its own class it will always return 'ZActiveRecord'...

As another poor fool who's spent hours trying to put together an ActiveRecord class in PHP5, I know exactly what Jared is getting at.

Mon, 05 Dec 2005 at 18:39:06 GMT Link


14.Peter Hodge said:

AFAIK there is no way to know if a statically called function is called as part of a child class. As far as I'm concerned, that's a fairly major limitation, so I requested the addition of a new magic constant to contain this information (well actually two while I was at it). If you think you deserve to know your child class's name then you can vote on the bug/feature request here: http://bugs.php.net/bug.php?id=34804 .

Tue, 06 Dec 2005 at 08:42:11 GMT Link


15.Jacob Wright said:

I have also created an ActiveRecord implementation and came up against this same wall. I would really like to know how Zend is using Person::findAll(), or perhaps that is just an example of how it should be, and they haven't gotten to the implementation yet.

Wed, 07 Dec 2005 at 14:13:46 GMT Link


16.Leendert said:

I've also written an ActiveRecord implementation, which you can find here:

http://www.daholygoat.com/jaws/html...n=Page&id=8

As far as I know, it is not possible to get the class name for the inherited class that it is being called in. It's been marked as a bug in PHP once, but it was assigned to "bogus", so I doubt Zend actually has an implementation of ZActiveRecord yet. In my getAll() I simply use get_class() on $this, which I'm able to do since I don't have a static getAll() function, so I have to instantiate with a "dumb" object anyway.

Wed, 07 Dec 2005 at 15:47:46 GMT Link


17.Brad Pauly said:

Add my name to the list of people curious about how they are doing this. I played around with an ActiveRecord class a couple weeks ago and found the same bug that Peter mentioned.

Thu, 08 Dec 2005 at 18:07:38 GMT Link


18.Jake Grimley said:

This rather pedantic implementation point is interesting from a wider view because it implies one of the following is true:

1. All of the above who tried to implement an ActiveRecord class and hit this stumbling block missed something subtle or obvious. (unlikely)

or

2. Zend are actually planning to make a fix to PHP in order to make this possible. (unlikely - doesn't tie into their stated plans)

3. Zend are throwing out blue-sky usage examples for classes that only exist in theory to date. Like the rest of us, they might have presumed this would be straightforward to implement, without actually trying it yet.

Anyway -- I hope they get it working, because in theory it all sounds great.

Fri, 09 Dec 2005 at 13:53:43 GMT Link


19.Clint Lenard said:

I can't wait for the Framework. I think it will push more hosts to upgrade to 5.1, finally. Plus, with all these companies and Individuals behind it, it has to be good!

What I'd like to see is some Video's, though. I think TurboGears and Ruby on Rails got popular through their Videos, more than the Frameworks themselves (yeah, I know people will scorch me for that, I could care less, It's true). This is just another example of the future of the Web and Video together. I think that's why PHP got popular too, with Companies like VTC putting out Tutorials in Video (only $30 a month). Hopefully Zend or someone will put out Video's for FREE though. Yeah, I know PHP|Arch has Video's -- but they're so d@mned expensive!

So, hopefully Zend has something in the Works on how to use the Framework and also Video's with Example Projects. The more Video's - the better. People can read all day long but in the end, they'll watch Video rather than read if It's available.

I just thought I'd give some input... :-)

Wed, 14 Dec 2005 at 21:00:10 GMT Link


20.johnpipi said:

I too have made an ActiveRecord class:

http://svn.phpontrax.com/browser/tr...tive_record.php

I actually made a whole framework myself

PHP on Trax (http://www.phpontrax.com)

before I knew anything about this Zend framework. The AR I made does it how Leendert did you have to instantiate a object every time no Object::find_all(), this is because you have to use get_class($this) to get the child classes name. This should really be fixed to return the proper class name, then you could do the Object::find_all(). Anyways my AR works great you can do all the table associations (belongs_to,has_many,has_one,has_and_belongs_to_many).

as well as all the CRUD stuff. Its funny all the above examples are how my framework already works... the syntax is very similar.

Sun, 18 Dec 2005 at 22:40:09 GMT Link


21.Shekar C. Reddy said:

I see a wide array of benefits to using the Zend Framework:

1. Free license

2. Entire framework created from scratch - no obsolete code over-head hanging around for backward compatibility

3. Support and insistence of PHP 5

4. Extreme simplicity - easy to learn/use

5. Light weight - so it can be used on very large websites

6. Free from IP infringement issues - very important!

7. Use only what you need in your logic. This feature used to be called serviced-based architecture (SBA) - instantiate only what you want on a need-to-use basis. I worked on SBA on PowerBuilder/PFC before and can say it is the most optimal architecture. This keeps the performance at its peak (can be used even on large websites) as we don't need to load/instantiate unnecessary classes into memory. If in doubt, try defining a couple of classes inside a single file, include it in another PHP file and run the debugger - you would notice it pre-loads each and every class found in the file into memory although class definitions themselves are not executable unless invoked/instantiated.

I only hope they don't restrict us from using templating engines such as Smarty, Savant...

Shekar

Sun, 01 Jan 2006 at 14:05:06 GMT Link


22.Linas said:

Will Zend Framework support php 4.x ?

Thu, 05 Jan 2006 at 11:56:48 GMT Link


23.vjoshi said:

I am curious as whether the framework is comparable to j2ee or .net. I mean is it a enterprise application framework that can be stateful and manage persistant data - the business logic layer or is it a web application framework for just the presentation layer of an enterprise application where the business end is in j2ee or .net

cheers

Tue, 10 Jan 2006 at 00:44:44 GMT Link


24.vjoshi said:

sorry for the double post but i forgot the diagram above, but if it is enterprise - then how is state preserved - i am not talking about db data but in the business logic. i thought php was more a page oriented language - go from one page to another to run the scripts on those pages. How do you make async. scripts similar to event handling or mvc patterns

cheers

Tue, 10 Jan 2006 at 00:47:50 GMT Link


25.Peter Pistorius said:

In response to "vjoshi:"

Um... No? Wait! Wait! Yes? NO! Wait! No? What?

Tue, 10 Jan 2006 at 07:46:39 GMT Link


26.David Coallier said:

In response to Linas about php 4.x.

Personally, php5 has many many improvements over php4 on many points, as well as OOP and security (even though php5.0.x is still fun..) but in my humble opinion, I am pretty sure anything can support php4, but should it. Where will be the point of using a php5 oriented/scaled framework if you destroy it with functional fake OO php4 code. So my answer would be no :)

Mon, 16 Jan 2006 at 15:02:42 GMT Link


27.John Peterson said:

This code is impossible with the current php5:

class Person extends ZActiveRecord {}

$people = Person::findAll();

You have to instantiate a object every time no Object::findAll(), this is because you have to use get_class($this) to get the child classes name. This should really be fixed to return the proper class name, then you could do the Object::findAll(). So with the current versions of php5 you would be forced to do the follow:

$person = new Person;

$people = $person->findAll();

Thu, 26 Jan 2006 at 23:31:33 GMT Link


28.Chris Shiflett said:

Hi John,

I have no experience trying to implement ActiveRecord, and I'm not at all involved with that part of the framework. However, I am curious to know what the disadvantages of instantiating what some people call a "dummy" object are.

(P.S. You're right that the code is currently impossible (aside from some clever hacks), and based on Mike's public comments, it seems likely that PHP will support this in the future.)

Thu, 26 Jan 2006 at 23:37:54 GMT Link


29.Aaron Wormus said:

A clever hack:

Assuming that a Rails-like file structure is enforced (i.e., the inheriting class is called Person.php something like this could be done:

class ZActiveRecord {

static function findAll()

{

$model = get_calling_model_name();

//..do magic lookup with DB table Person (or People)

}

static function get_calling_model_name()

{

$filename = basename($_SERVER['SCRIPT_FILENAME']);

list($model, $ext) = split("\.", $filename);

return $model;

}

}

If you have a file called Person.php that looks like this:

require("ZActiveRecord.php");

class Person extends ZActiveRecord{}

echo Person::basename();

and you get "Person" from the filename. Rails may not be doing anything more magical than this, since it does enforce the naming convention.

But maybe I should run `sed s/clever/ugly/` on this post?

Fri, 10 Feb 2006 at 02:07:40 GMT Link


30.the ~e~ said:

well, I have tried zend, and someones post above is right, PHP on Rails (a.k.a. Trax) has stuff Zend could really benefit from. Chris, can you compare some other frameworks?

Thu, 25 May 2006 at 07:25:37 GMT Link


Hello! What’s your name?

Want to comment? Please connect with Twitter to join the discussion.