PhpRiot
Blog Archive
Buy My Book
Practical Web 2.0 Applications with PHP

Practical Web 2.0 Applications with PHP

Want to assert yourself as a cutting–edge PHP web developer? Take a practical approach...
PhpRiot Latest Blog Posts

Eight part series on JavaScript development with Prototype launched!

I have today launched the first article in my eight part series about the Prototype JavaScript framework.

There will be a new article released each week for the next seven weeks, covering a wide range of topics, such as creating classes and event handling with Prototype.

While the content of these articles is not strictly related to PHP, hopefully as PHP developers you will find them useful anyway in building your own modern day web applications. The final article in this series will cover an extensive example of using Prototype, in which PHP will be used on the server-side.

In addition to being able to read each article for free online, you can purchase a PDF copy of each article for a nominal fee ($5), or purchase the entire 8-part series for $12. If you purchase the entire series you will be notified when each subsequent article is released, allowing you to then download it.


Zend Framework 1.5.0 released

Zend Technologies today released version 1.5.0 of its Zend Framework, which is good for developers since it brings new features and bug fixes, but more work if you try to keep your sites up-to-date with the latest version.

There are some new components available in this version, such as support for OpenID, the new Zend_Form component, and the new Zend_Layout component.

Although I haven't yet used Zend_Layout, reading through the documentation I'm finding it hard to see what real use it provides. To me it looks like an extra presentation layer that doesn't really achieve anything that can't be done using Zend_View (or a custom implementation, such as using Smarty).

The idea of Zend_Form is good, although I think if I ever use this component I would just use it for form processing and not use it to generate my HTML output. Doing so blurs the lines once again between presentation and application logic. Additionally, defining validation rules in a form processor is not always an ideal solution. For instance, if you want to use similar versions of the form on different areas of the site (such as the public area of the site and in an administration area), you may find yourself duplicating validation rules. The alternative to this would be define them in the central class that manages the respective data.

Another improvement is in the Zend_Search_Lucene component. According to the press release, "Now it is possible to implement advanced queries using wildcards, date ranges, and fuzzy searches from within a Zend Framework application". This component is extremely powerful and these new features sound like they will be really useful.

The Zend_Pdf component has been improved so it now supports UTF-8. In my own dealings with this component in prior versions, it felt like the overall component was very immature compared to other ZF components (in areas such as API/PhpDoc comments, coding style and in the official documentation). Not only that, it seemed like a lot of obvious features were missing. I'd like to be proven wrong on this as I really want to be able to use it.

Finally, there are notes about the Gdata (used for Google services) handling being improved. While improvements are good, my general feeling on all these third-party services in Zend Framework is simply that they shouldn't be in there. The ZF should provide the tools to solve problems, it shouldn't solve specific problems such as how to communicate with Google, Amazon, Delicious, Flickr or Yahoo!.


Using the PHP 5 Iterator interface with Smarty

The PHP 5 Iterator interface is very useful for defining custom behaviour for looping over objects, however I just noticed that looping over such objects in Smarty will not work correctly.

Smarty will in fact cast an object back to an array, so when you loop over it, the template will loop over the object's properties (as opposed to using the rules defined by the Iterator methods).

For example, I used code similar to the following to create a class over which I can loop. This lets me loop over the protected $_data array.

<?php
    class MyClass implements Iterator
    {
        protected $_data = array();
 
        public function rewind()
        {
            reset($this->_data);
        }
 
        public function current()
        {
            return current($this->_data);
        }
 
        public function key()
        {
            return key($this->_data);
        }
 
        public function next()
        {
            return next($this->_data);
        }
 
        public function valid()
        {
            return $this->current() !== false;
        }
 
        public function size()
        {
            return count($this->_data);
        }
    }
?>

What I want to be able to do is loop over each element in the array with the following code:

{foreach from=$myClassObj item=row}
    {$row}
{/foreach}

However, this will not work as expected in Smarty. To get around, I implemented as method called getData() which simply returned the array. This meant my template had to look as follows:

{foreach from=$myClassObj->getData() item=row}
    {$row}
{/foreach}

It's a bit annoying to have to do this, since in some ways it defeats the purpose of the using the Iterator interface, but I guess sometimes small sacrifices need to be made.

Permalink | 1 comment | Tagged in Smarty