A first look at the Zend Framework
(Page 3 out of 4)Zend_Feed
The Zend Framework makes reading RSS or Atom feeds much easier by supplying an inbuilt component, Zend_Feed, which automatically parses any feed and changes it into a native PHP object. The below example demonstrates this:
// Use Zend_Feed
require_once 'Zend/Feed.php';
// Fetch the latest Slashdot headlines
try {
$slashdotRss = Zend_Feed::import('http://rss.slashdot.org/Slashdot/slashdot');
} catch (Zend_Feed_Exception $e) {
// feed import failed
echo "Exception caught importing feed: {$e->getMessage()}\n";
exit;
}
// Initialize the channel data array
$channel = array(
'title' => $slashdotRss->title(),
'link' => $slashdotRss->link(),
'description' => $slashdotRss->description(),
'items' => array()
);
// Loop over each channel item and store relevant data
foreach ($slashdotRss as $item) {
$channel['items'][] = array(
'title' => $item->title(),
'link' => $item->link(),
'description' => $item->description()
);
}
?>
Initially, I was unable to get the code above working, but after several fixes in the framework I was able to get it working. First, you have to fix a constant not defined problem by changing, on line 373 in Zend/Uri/Http.php, Zend_InputFilter to Zend_Filter both times, so that the line reads:
After that you have to remove (or comment out) a whole block of code in Zend/Filter.php, lines 345 to 346. Once you've done that, the above example will work (although a few validation checks might be skipped now).
The feed object also supports feed auto-discovery by automatically finding feeds in a web page, like most popular blogging tools do. The below example demonstrates this:
// Use Zend_Feed
require_once 'Zend/Feed.php';
$feedArray = Zend_Feed::findFeeds('http://www.example.com');
?>
Let's skip the Zend_HttpClient component as it's a pretty standard HTTP client, and move on to the Zend_InputFilter, which seems quite promising.
Zend_InputFilter
The Zend_Inputfilter comes with three different filtering methods, namely whitelist, blind and blacklist filtering.
Whitelist filtering methods begin with the is prefix, and only return data if it matches the right criteria, and will otherwise return false. See the below example for a good demonstration:
$filterPost = new Zend_InputFilter($_POST);
if ($alphaName = $filterPost->isAlpha('name')) {
/* $alphaName contains only alphabetic characters. */
} else {
/* $alphaName evaluates to FALSE. */
}
?>
Blindlist filtering on the other hand simply remove all the characters that don't match the right criteria, e.g.
/* $_POST['username'] = 'Chris123Shiflett'; */
$filterPost = new Zend_InputFilter($_POST);
$alphaUsername = $filterPost->getAlpha('username');
/* $alphaUsername = 'ChrisShiflett'; */
?>
Blacklist filtering removes data that does match right criteria, and leaves the rest intact, like so:
/* $_POST['comment'] = 'I love PHP!'; */
$filterPost = new Zend_InputFilter($_POST);
$taglessComment = $filterPost->noTags('comment');
/* $taglessComment = 'I love PHP!'; */
?>
The best method of filtering is obviously the whitelist filtering, but the other two filtering methods are properly very handy as well.
Another major advantage of the Zend_InputFilter component is that it removes access to the raw data. In the above examples, the $_POST variable will be set to null, and to retrieve any POST data you must use the filtering methods. It's still possible to get the raw data, through the getRaw() method, but this makes it much easier to see when you're using raw data.
Let's look at one more component in the framework: Zend_Service, which is used to query popular web services, like Flickr, Amazon and Yahoo.
March 6th, 2006 at 6:27 pm
So, compare these four lines:
$select->from(’round_table’, ‘*’);
$select->where(’noble_title = ?’, ‘Sir’);
$select->order(’first_name’);
$select->limit(10,20);
“SELECT * FROM round_table WHERE noble_title = ‘Sir’ ORDER BY first_name limit 10,20″
How is the 4 line object statement (copied below) “so much easier” than a single line SQL statement?
The SQL can be formatted however the user wishes, remaining readable, and happily including variables.
SELECT * FROM round_table
WHERE noble_title = ‘Sir’
ORDER BY first_name
LIMIT 10,20
The SQL-way has less typing, reads as an English sentence, both enhancing creation and maintenance. Importantly, the SQL select statement can be made in one string, so errors where a piece of the object has not been updated since a previous call, but the SQL still executes, are not possible.
For getting the job done, bog-standard PHP still seems to rule for me. My prediction is that the X on X fad will maintain a noisy list of supporters, but the 80% apps that make money, help us discover things, and change the world, are going to remain, like php, simple enough to get the job done, and leave SQL alone to do its lovely specialism.
I think Andy is right: Scaffolding saves 5 minutes once. Php saves you those minutes every hour for the rest of your coding. It stays out of your way while you work, understanding that no one can do the work for you, least of all a dumb language. Work remains a product of mass acceleration and distance: that makes it fundamentally hard.
March 6th, 2006 at 8:19 pm
Indeed it will be very interesting to see where this framework will go. I also like the input filtering approach in which the access to the raw data is removed. Marco Tabini wrote about this (or a similar) concept in the feb issue of PHP architect, calling it poka-yoke, which is Japanese for “fail-safe mechanism.”
March 8th, 2006 at 8:46 pm
Zend Framework - wejście smoka
Kilka dni temu została opublikowana pierwsza wersja zapowiadanego od dluższego czasu Zend Framework oznaczona numerkiem 0.1.1. Podszedłem do tego dość sceptycznie i z dystansem, bo publikacja tak wczesnej wersji mogła okazać się niewypałem, a …
August 6th, 2006 at 8:35 pm
Ok, so the argument to fetchAll is a string. So, like,
$result = $db->fetchAll(”SELECT * FROM round_table WHERE noble_title = ‘Sir’ ORDER BY first_name limit 10,20″)
does what you want, right?