Saturday, June 23, 2007

Taking a look at ten different PHP frameworks

Zend Framework

The Zend Framework is still pretty new, and very beta, so there's still a few things to come (like a user authentication module), but it does already have quite a lot. Although it doesn't have a lot of checks in the chart, the Zend Framework does include other modules, to handle PDF files, RSS feeds, Web Services (Amazon, Flickr, Yahoo), and more. The Zend Framework also includes several different database objects, making it extremely simple to query your database, without having to write any SQL yourself.

At the moment the Zend Framework doesn't come with a full-blown ORM, but the framework developers are still debating whether to add an ORM layer or not. This might be something we see in the future, and you can always write your own ORM implementation, and have it added to the framework, as it accepts user contributions.

The Zend Framework is looking quite promising, and is definitely working to fix the common PHP problems. The stable version will probably have a lot more checks in the comparison chart!

CakePHP

CakePHP is mostly an advanced MVC framework, with a few extra modules added on top. It can handle most of the database stuff for you, and it includes support for Ajax and data validation. It also has a unique user authentication module called 'Access Lists', which can be used to give different users access to different parts of your CakePHP website.

This framework seems quite thorough and ready for use, although the CakePHP website is extremely confusing. There doesn't seem to be a stable version yet, which is a bit surprising since it's been in development for months now, but I guess it's probably possible to use the beta version on a production website as well.

Symfony Project

As you can see from the chart the Symfony Project seems like a very extensive framework, and it even includes a full-blown ORM, called Propel, which is another open source project and probably one of the best ORM solutions for PHP. Symfony also includes Creole for the database abstraction layer and Mojavi for the Model-View-Controller model layer. Because it simply re-uses these other projects, instead of re-writing them, this framework probably has the most extensive functionality, and this is clearly shown in the chart.

The only two downsides to this framework is that fact that it can't be run on PHP4, but this is pretty logical considering it needs much of PHP5's new features, and that it's so complicated. Most of the tasks, like paging, are much more complicated in Symfony than in other frameworks, and simplicity definitely doesn't describe this framework.

Seagull Framework

The Seagull framework seems like a pretty good framework, and it comes with quite a lot of features. Another positive thing is that it runs on PHP4 as well, which is a really good thing in my book, as I still use PHP4 and most web hosts don't support PHP5 yet.

Unfortunately Seagull doesn't come with any Ajax support, which is a bit of a shame, but a simple Google search returns a tutorial on adding Ajax support yourself, and it's likely it will be added in the future.

WACT - Web Application Toolkit

WACT looks quite promising, and it has the basic things a framework needs, like MVC, database objects and a template engine, but that's about it. It doesn't have any extra modules, and it definitely lacks a few inbuilt things, like Ajax, caching, an authentication module and more.

WACT does have one of the best template engines though, and it even allows you to create your own template tags to add certain functionality.

Prado

Prado is a very unique framework, and it offers some really cool features. It doesn't come with inbuilt support for MVC (from what I can tell), but it does have all kinds of components to handle most of the usual PHP tasks, and what's even neater it that it makes event-driven scripts possible. For example, it's possible to set an onClick event on a HTML button that links to a PHP function. This closely resembles ASP.NET, and Prado contains more ASP.NET components, like a GridView and a Repeater Control.

By looking at the comparison chart it might seem that Prado is a bit weak compared to the others, but this isn't true. Because Prado is so unique, it doesn't score a lot of points on the chart, but in reality it is very powerful.

PHP on Trax

This framework seems to be a PHP port of Ruby and Rails, and used to be called PHP on Rails. Unfortunately documentation is quite sparse, so it's hard to determine what this framework offers, but its documentation does mention ActiveRecord, so it probably offers ActiveRecord, just like RoR. Another thing it includes is inbuilt support for Ajax, and the two demos that can be found on the website clearly show this.

PHP on Trax is on the right way, but it's still missing a few things, like an authentication module and a caching module. It doesn't have any extra modules either, so it's all a bit basic at the moment.

ZooP Framework

The ZooP Framework is really feature-packed, and as the chart clearly shows, it supports almost anything, except for an ORM layer. What's even more surprising is that it runs on PHP4, which is really exceptional considering all the functionality it offers.

Another thing ZooP offers is inbuilt GUI controls, making it very easy to create forms quickly. It supports most of the basic form elements, and that advantage of using GUI controls over regular HTML is that you can tightly integrate validation logic with the GUI controls.

eZ Components

ez Components isn't so much a framework, but more of a collection of components, and that's probably why it doesn't come with inbuilt support for MVC.

It comes with most of the usual components, like a database component and a caching component, but it's also lacking a few things. For instance, it doesn't have a ActiveRecord component or a authentication component. On the other hand, it does have a few other extra components, like an e-mail component, but there aren't many, and other frameworks (like the Zend Framework) offer more.

CodeIgniter

CodeIgniter is a relatively new framework, by the makers of ExpressionEngine, and it looks quite promising. Unfortunately, it doesn't handle multiple databases yet, although that is in the works.

It's inspired by Ruby on Rails, and it offers much of the same functionality, like scaffolding. It has excellent documentation, and they've even got video tutorials to get you going.

And the winner is...

I'm going to have to disappoint you here, as there isn't a winner. Each framework offers different functionality, and there is no clear winner. Just like most things, it comes down to preference and personal choice. I highly suggest you look through the documentation of each framework above, and decide yourself which framework you like best.

Personally, I really like the Zend Framework at the moment, but as I've been investigating each framework, I've seen many other interesting features, like Prado's events or Symfony's ORM implementation.

If you absolutely can't decide on what framework to use, you can always do what most people do: write your own. It may take more time, but it's a hell of a lot fun.

If you have any comments or questions, feel free to drop them in the comments below or join us at PHPit Forums. Also, if you find any incorrect information in the chart or this article, please let me know so I can correct it.

Taking a look at ten different PHP frameworks

Introduction
PHP frameworks are the latest hot topic in the PHP community, and new frameworks are being released daily. With over 40 frameworks available it's difficult to decide which framework works best for you, especially as each framework offers different functionality.

In this article we will look at ten popular frameworks, and compare them to each other. First I will give you a general chart which gives you a quick overview of all the frameworks, and after that we'll go through each framework and discuss it in short.

A Quick Overview
Below you can view a chart of the ten frameworks, and what functionality each frameworks offers.

Framework PHP4 PHP5 MVC1 Multiple DB's2 ORM3 DB Objects4 Templates5 Caching6 Validation7 Ajax8 Auth Module9 Modules10

Zend Framework - - - - -
CakePHP - -
Symfony Project - - -
Seagull Framework -
WACT - - - - -
Prado - - - -
PHP on TRAX - - - - -
ZooP Framework -
eZ Components - - - - -
CodeIgniter - - -

#1: Indicates whether the framework comes with inbuilt support for a Model-View-Controller setup.
#2: Indicates whether the framework supports multiple databases without having to change anything.
#3: Indicates whether the framework supports an object-record mapper, usually an implementation of ActiveRecord.

#4: Indicates whether the framework includes other database objects, like a TableGateWay.
#5: Indicates whether the framework has an inbuilt template engine.
#6: Indicates whether the framework includes a caching object or some way other way of caching.
#7: Indicates whether the framework has an inbuilt validation or filtering component.
#8: Indicates whether the framework comes with inbuilt support for Ajax.
#9: Indicates whether the framework has an inbuilt module for handling user authentication.

#10: Indicates whether the framework has other modules, like an RSS feed parser, PDF module or anything else (useful).

Source

Wednesday, June 20, 2007

JQuery - Javascript Framework

jQuery is an amazing JavaScript library that makes it easy to create wonderful web effects in just a few lines of code. As the website says:”

“jQuery is a JavaScript library that takes this motto to heart: Writing JavaScript code should be fun. jQuery achieves this goal by taking common, repetitive, tasks, stripping out all the unnecessary markup, and leaving them short, smart and understandable.”

Maybe you are thinking… “Why I would need another JavaScript library”? Just give a try and you will see how simple and powerful it is even if you have already used Moo.fx, Scriptaculous, TW-SACK or Prototype.

Why I should use jQuery?

Simple. In just one glance at the source code of a page using jQuery you’ll see how easy it is to use, how much it accomplishes in so few lines of code, and how graceful it is.

My mind was opened one day when I stumbled across some code written with jQuery. I was flipping through the RSS feeds and reading my daily dose of web design blogs when I came across an example of JavaScript loveliness that used jQuery. Truth be told, the code on that site had some browser related bugs… but the concept was something I hadn’t seen before.
What about the code?

The code looked almost simple. Like nothing I had seen before. It made sense.

I started reading through the documentation and was amazed to see how much could be done with so little extra code.

When you can use jQuery?

You should use jQuery when you need:

* A small library that gives you powerful control over the Document Object Model
* With very little effort or work on your part

Or

* Quick access to AJAX
* Without a lot of bloat (overhead - wasted code)
* And some basic animation effects to spice things up

But…

If you need super fancy effects for animation, drag and drop, and super smooth animation then you’ll probably want to use Prototype and one of the many great library created to enhance the effects.

Where to get it?

You can download the source code (15k), a lot of plugins and read some excellent tutorials at the jQuery website. jQuery was created by John Resig.

Whats Ajax?

A Primer on the Ajax Phenomenon

With all the hype about "Ajax web applications", you could be forgiven for assuming Ajax is some radical new plugin, akin to Macromedia Flash. And you would therefore be under the impression that Ajax is out of bounds until you upgrade your "so last month" browser, or at least download the coveted "Ajax plugin".

Good news: You can run Ajax right now. At least, assuming you have a web browser from the past few years - IE, Firefox, Safari, or Opera. Have you seen Google Maps or perhaps GMail? They're both Ajax applications. So are Zuggest and the AjaxPatterns Wiki Demo .

What do they all have in common? All these applications take a great leap forth towards the richness of standard desktop applications. No longer are you forced to wait five seconds for the page to reload every time you click on something. Ajax applications change in real time. They can let you drag boxes around, they can refresh themselves with new information, they can completely re-arrange the page without clearing it. And there's no special plugin required. Ajax is just a style of design, one that milks all the features of modern browsers to produce something that feels less web and more desktop.

Applications can act more like and be developed more like the days of Visual Basic, Delphi, PowerBuilder, C++ with GUI frameworks, etc. Thus, it is 90's GUI features but with a web browser: it strives to meld the best of web and the best of desktop GUI's. Web browser standards were originally designed for e-brochures only. Business forms and other needs were hacked into this e-brochure framework over time and it is clear that this after-thought retrofitting for different purposes has been ugly. Ajax attempts to remedy this.

Some of the characteristics of Ajax applications include:

* Continuous Feel: Traditional web applications force you to submit a form, wait a few seconds, watch the page redraw, and then add some more info. Forgot to enter the area code in a phone number? Start all over again. Sometimes, you feel like you're in the middle of a traffic jam: go 20 metres, stop a minute, go 20 metres, stop a minute ... How many E-Commerce sales have been lost because the user encountered one too many error message and gave up the battle? Ajax offers a smooth ride all the way. There's no page reloads here - you're just doing stuff and the browser is responding. The server is only telling the screen what changed rather than having it redraw the whole screen from scratch.

* Real-Time Updates: As part of the continous feel, Ajax applications can update the page in real-time. Currently, news services on the web redraw the entire page at intervals, e.g. once every 15 minutes. In contrast, it's feasible for a browser running an Ajax application to poll the server every few seconds, so it's capable of updating any information directly on the parts of the page that need changing. The rest of the page is unaffected.

* Graphical Interaction: Flashy backdrops are abundant on the web, but the basic mode of interaction has nevertheless mimicked the 1970s-style form-based data entry systems. Ajax represents a transition into the world of GUI controls visible on present-day desktops. Thus, you will encounter animations such as fading text to tell you something's just been saved, you will be able to drag items around, you will see some static text suddenly turn into an edit field as you hover over it.

* Language Neutrality - Ajax strives to be equally usable with all the popular languages rather than be tied to one language. Past GUI attempts such as VB, Tk, and Swing tended to be married to one specific programming language. Ajax has learned from the past and rejects this notion. To help facilitate this, XML is often used as a declarative interface language.

To prevent any confusion, these things are not characteristic of Ajax:

* Non-Proprietary: "Ajax" is perhaps one of the most common brand names in history, but in context of web design, "Ajax" is neither the name of a company nor a product. It's not even the name of a standard or committee. It's a label for a design approach involving several related technologies.

* Plugin-Based: Ajax applications do not require users to install browser plugins, or desktop software for that matter.

* Browser Specific: As long as the user is working with a relatively recent, mainstream, browser (say 2001+), the application should work roughly the same way. Browser-specific applications somewhat defeat the purpose of Ajax.

Sunday, June 17, 2007

Square Peg, Round Hole In PDF With PHP



Lines aren't the only thing you can draw - circles and rectangles also figure prominently on the menu. Take a look at the following example, which demonstrates.

php
// create handle for new PDF document
$pdf = pdf_new();

// open a file
pdf_open_file($pdf, "shapes.pdf");

// start a new page (A4)
pdf_begin_page($pdf, 595, 842);

// set a fill colour
pdf_setcolor($pdf, "fill", "rgb", 1, 1, 0);

// set a stroke colour
pdf_setcolor($pdf, "stroke", "rgb", 0, 0, 0);

// draw a rectangle
pdf_rect($pdf, 50, 500, 200, 300);
pdf_fill_stroke($pdf);

// set a fill colour
pdf_setcolor($pdf, "fill", "rgb", 0, 1, 0);

// set a stroke colour
pdf_setcolor($pdf, "stroke", "rgb", 0, 0, 1);

// draw a circle
pdf_circle($pdf, 400, 600, 100);
pdf_fill_stroke($pdf);

// end page
pdf_end_page($pdf);

// close and save file
pdf_close($pdf);
?>

Here's the output:

1225_4 (click to view image)

In this case, the pdf_rect() function has been used to draw a rectangle, given the coordinates of the lower left corner and the height and width. This rectangle has then been filled and outlined in two different colours, via the pdf_fill_stroke() function.

pdf_setcolor($pdf, "fill", "rgb", 1, 1, 0);

pdf_setcolor($pdf, "stroke", "rgb", 0, 0, 0);

pdf_rect($pdf, 50, 500, 200, 300);

pdf_fill_stroke($pdf);

Circles are handled by the pdf_circle() function, which accepts three
arguments: the X and Y coordinates of the circle center, and the length of the circle radius.

pdf_circle($pdf, 400, 600, 100);

This ability to draw geometric shapes on the fly can come in handy in a number of different situations. Consider the following one, in which a couple of "for" loops have been combined with the pdf_lineto() function to generate a PDF line grid.

// create handle for new PDF document
$pdf = pdf_new();

// open a file
pdf_open_file($pdf, "grid.pdf");

// start a new page (A4)
pdf_begin_page($pdf, 595, 842);

// set a stroke colour
pdf_setcolor($pdf, "stroke", "rgb", 0, 0, 0);

// draw vertical lines (move along X axis)
for ($x=0; $x<=595; $x+=20)
{
pdf_moveto($pdf, $x, 0);
pdf_lineto($pdf, $x, 842);
pdf_stroke($pdf);
}

// draw horizontal lines (move along Y axis)
for ($y=0; $y<=842; $y+=20)
{
pdf_moveto($pdf, 0, $y);
pdf_lineto($pdf, 595, $y);
pdf_stroke($pdf);
}

// end page
pdf_end_page($pdf);

// close and save file
pdf_close($pdf);
?>

Here's the output:

1225_5 (click to view image)

Heaven Is A Place On Earth

You can set document information with the pdf_set_info_*() family of functions; this information identifies the document creator, title and content. The following example demonstrates:

// create handle for new PDF document
$pdf = pdf_new();

// open a file
pdf_open_file($pdf, "philosophy.pdf");

// start a new page (A4)
pdf_begin_page($pdf, 595, 842);

// set document information
pdf_set_info_author($pdf, "William Shakespeare"); pdf_set_info_creator($pdf, "William Shakespeare"); pdf_set_info_title($pdf, "Hamlet"); pdf_set_info_subject($pdf, "Act I Scene 5"); pdf_set_info_keywords($pdf, "Horatio Hamlet philosophy");

// get and use a font object
$arial = pdf_findfont($pdf, "Arial", "host", 1); pdf_setfont($pdf, $arial, 10);

// print text
pdf_show_xy($pdf, "There are more things in heaven and earth, Horatio,", 50, 750); pdf_show_xy($pdf, "than are dreamt of in your philosophy", 50, 730);

// end page
pdf_end_page($pdf);

// close and save file
pdf_close($pdf);
?>

And now, when you view the dynamically-generated PDF file in Acrobat Reader, you'll see this information in the Document Properties window.

1225_6 (click to view image)

Generate PDFs with PHP

Pretty As A Picture

Now, that was a very simple example - but PHP's PDF extension allows you to do a lot more than just write text to a page. Since a picture is worth a thousand words, consider this next example, which demonstrates the process of adding an image to your newly-minted PDF document.

// create handle for new PDF document
$pdf = pdf_new();

// open a file
pdf_open_file($pdf, "philosophy.pdf");

// start a new page (A4)
pdf_begin_page($pdf, 595, 842);

// get and use a font object
$arial = pdf_findfont($pdf, "Arial", "host", 1); pdf_setfont($pdf, $arial, 10);

// print text
pdf_show_xy($pdf, "There are more things in heaven and earth, Horatio,", 50, 750); pdf_show_xy($pdf, "than are dreamt of in your philosophy", 50, 730);

// add an image under the text
$image = pdf_open_image_file($pdf, "jpeg", "shakespeare.jpg"); pdf_place_image($pdf, $image, 50, 650, 0.25);

// end page
pdf_end_page($pdf);

// close and save file
pdf_close($pdf);
?>

Here's the PDF output:

1225_2 (click to view image)

Most of the magic here happens via the pdf_open_image_file() and
pdf_place_image() functions. The first one accepts an image type - GIF, JPEG, TIFF or PNG - and file name as arguments, and returns an image handle, which may then be re-used multiple times in the document.

The image handle returned in the previous step can be used by the pdf_place_image() function, which actually takes care of positioning the image at a particular point on the page. The coordinates provided to this function (the second and third arguments) refer to the position of the lower left corner of the image, while the fourth argument specifies the scaling factor to use when displaying the image (a scaling factor of 1 will show the image at actual size, while a factor of 0.5 will reduce the image to half its size).

The Shortest Distance Between Two Points

Why stop there? PHP's PDF extension comes with a whole bag of functions designed to let you draw lines, circles and other shapes in your PDF document. Consider the following example, which demonstrates the process of drawing a line.

// create handle for new PDF document
$pdf = pdf_new();

// open a file
pdf_open_file($pdf, "letterhead.pdf");

// start a new page (A4)
pdf_begin_page($pdf, 595, 842);

// get and use a font object
$arial = pdf_findfont($pdf, "Arial", "host", 1); pdf_setfont($pdf, $arial, 12);

// set a colour for the line
pdf_setcolor($pdf, "stroke", "rgb", 0, 0, 0);

// place a logo in the top left corner
$image = pdf_open_image_file($pdf, "jpeg", "logo.jpg"); pdf_place_image($pdf, $image, 50, 785, 0.5);

// draw a line under the logo
pdf_moveto($pdf, 20, 780);
pdf_lineto($pdf, 575, 780);
pdf_stroke($pdf);

// draw another line near the bottom of the page pdf_moveto($pdf, 20, 50); pdf_lineto($pdf, 575, 50); pdf_stroke($pdf);

// and write some text under it
pdf_show_xy($pdf, "Confidential and proprietary", 200, 35);

// end page
pdf_end_page($pdf);

// close and save file
pdf_close($pdf);
?>

Here's what you should see in the PDF document:

1225_3 (click to view image)

In this case, the process of drawing a line involves creative use of the pdf_moveto(), pdf_lineto() and pdf_stroke() functions.

In the example above, I'd like to draw a line from the position (20,780) to the new position (575, 780). In order to do this, I first need to place the cursor at the starting point (20,780), via a call to pdf_moveto().

pdf_moveto($pdf, 20, 780);

Next, I need to set the end point of the line, via pdf_lineto():

pdf_lineto($pdf, 575, 780);

Finally, the line is actually rendered using pdf_stroke().

pdf_stroke($pdf);

The stroke colour is set via a call to pdf_setcolor(), which accepts a number of parameters: the PDF document handle, whether the colour being set is for "stroke", "fill" or "both", the colour scheme to use (RGB or CMYK), and a list of colour values appropriate to the selected colour scheme.

pdf_setcolor($pdf, "stroke", "rgb", 0, 0, 0);

It's important to note that the list of colour values provided to
pdf_setcolor() must be specified in terms of percentage intensity - that is, the intensity of that colour, expressed as a percentage of the maximum intensity possible. So, if I wanted to set red (RGB: 255,0,0) as the stroke colour, my call to pdf_setcolor() would look like this:

pdf_setcolor($pdf, "stroke", "rgb", 1, 0, 0);

while a fill colour of yellow (RGB: 255,255,0) would be:

pdf_setcolor($pdf, "fill", "rgb", 1, 1, 0);

Saturday, June 16, 2007

What's new in PHP V5.2, Part 5: Tracking file upload progress

PHP V5.2 added hooks for developers to take advantage of tracking file upload progress in real time. This article, Part 5 of a five-part "What's new in PHP V5.2" series, shows how to monitor file uploads and write code accordingly, with the creation of a PHP progress bar.

Web 2.0 is the hottest buzzword on the Internet, with investors lining up to put money into any bucket labeled by it. There are many descriptive terms for the millions of Web sites and applications they house. With Web 2.0, we describe a category of Web sites that provide a voice to the millions of users of the Internet. Distinctive in that they all provide a venue for users to meet and share opinions and data relating to common interests, these sites generate enormous amounts of content rapidly.

Each of the users provides some kind of content -- reviews of coffee shops, routes to work, etc. YouTube is a great example of this, providing a place for people to upload videos and have other users watch them and provide feedback. YouTube is the current darling of the Web 2.0 observers, noting that YouTube's popularity grew faster than any site on the Internet to date. This popularity can be attributed to a great deal of varied content, along with the ability for users to lend their voices to the content in the form of comments. And not just comments -- users can even upload video comments in response to video.

Text fields, ahoy

Many Web sites that accept files sport the dreaded Browse button next to a text field urging users to upload each file one at a time. This can take a long time, especially in the case of video, or even photos or other items that come in a groups of smaller files. Since each file requires its own upload, it can be quite a chore. Given that uploading huge files can be tedious for impatient users, it is important to provide them positive feedback to keep them from giving up and going away.

Fortunately, PHP V5.2's new hooks into the file upload process allow us to show users in real time what is happening with their uploads. In this article, we will create a progress bar using PHP V5.2 for our users (see Download for source code).





Hook, line, and sinker

The new "hooks" in PHP V5.2 are actually data points that are available during the file transfer process if you have the right libraries installed and configured. They use a feature called the Alternative PHP Cache. When a PHP script receives an uploaded file, the interpreter will automatically check the $_POST array for a hidden field named APC_UPLOAD_PROGRESS, which becomes a cached variable, storing information about the upload so your scripts can access it. With this information cached and at your fingertips, you can give your users visual feedback to improve their user experience.

We will cover the implementation of the APC code in your HTML form, as well as identifying it in your PHP and how to access the cached information. There are many ways to represent this data — from Ajax to FLEX — but what we will focus on is how to prepare access to the data these front-end technologies will need.





Setting up

APC is not enabled by default in PHP V5.2. Since the new hooks are a part of APC, we need to make sure to install the extension and make it available to the PHP interpreter. This is accomplished by downloading the php_apc extension files. In our case, we are using an installation of WAMP, a freely available packaged PHP for Windows®, which includes Apache and MySQL. It offers a nice user interface and is easy to manage with menus that support configuration options.

To set up APC on WAMP:

  1. See Resources to download the libraries and WAMP.
  2. Install WAMP.
  3. Put the php_apc.dll file in the extensions folder for PHP. This is /php/ext by default.
  4. Use the system tray WAMP menu to select PHP settings>PHP Extensions>Add Extension.
  5. In the command-line interface that pops up, type php_apc.dll and press Enter.
  6. Using a text editor, open /php/php.ini and add the line apc.rfc1867 = on (it doesn't matter where). If you're trying to test locally and plan to upload large files so you can actually see progress, you'll also want to add the following directives: apc.max_file_size = 200M, upload_max_filesize = 200M, and post_max_size = 200M. Don't do this on a live production server, though, or you're likely to use up bandwidth and disk space allotments, not to mention slowing everyone else down to a crawl.
  7. Restart PHP.

APC should now be set up and initialized. The RFC1867 features of APC — the features that enable you to track file uploads — should now be enabled as an option, and you should be ready to look into our file uploads to enable real-time status.