Article

PHP5: Coming Soon to a Webserver Near You

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Next

Test Drive

As the preceding discussion suggests, the new features that PHP5 delivers are what you might call "advanced", in terms of the way we use PHP as a language. If you're not already used to PHP4's object model, or you have no experience using OOP in another language, you'll have to forgive me if the following examples go over your head. But if you've been stalling on learning about OOP in PHP, now would be a very good time to start, as the version 4 syntax is pretty simple.

Help is at hand, in the form of a selection of Advanced PHP Resources, as well as Kevin Yank's article Object Oriented PHP: Paging Result Sets, which serves as a great primer on PHP4's class syntax. For a more in-depth primer in Object Oriented Programming, I recommend Bruce Eckel's Thinking in Java (available for free in electronic form). In case you're wondering why I'm recommending a book on Java to PHP programmers, "Thinking in Java" provides a very solid summary of all the fundamental aspects of the object oriented paradigm, and the code that illustrates them is simple and to the point. Java's syntax is close enough to PHP4, OOP-wise, for someone with a grasp of the PHP syntax to understand what Java is doing.

That said, if OOP really turns you off, there are a few other new features in PHP5 that might interest you, particularly the areas of SQLite, exception handling, and SimpleXML, all of which I'll be looking at in a moment.

When it comes to installing PHP5, I'm going to leave it to the Advanced PHP Forum to help you out. Here, you'll find people who've successfully installed it under Linux, Mac OSX and Windows. I managed to install successfully under Suse 8.1 Linux and Windows 2000, the process being relatively painless and essentially the same as with PHP4 (see the manual on installation for more). I did hit a few problems with Windows XP, where Apache crashed upon running any PHP scripts, though that issue may be specific to my system.

One tip for Windows users (which comes under the heading "Quick and Dirty") is to copy iconv.dll from the php5/dlls to the php5/sapi directory (to deal with the fact that PHP is unable to find php4apache.dll).

Regarding Apache configuration, first, make sure you're using a 1.3.x release of Apache (as PHP4 and 5 have not been declared stable with Apache 2.x). In httpd.conf, where, for PHP4 you would normally have had something like:

 
LoadModule php4_module libexec/libphp4.so" # Unix  
#LoadModule php4_module "c:/php4/sapi/php4apache.dll" # Windows  

You now have:

 
LoadModule php5_module libexec/libphp5.so" # Unix  
#LoadModule php5_module "c:/php4/sapi/php4apache.dll" # Windows  

Note that on Windows, you're still using php4apache.dll with PHP5 (no, that's not a typo).

Be aware that the example code I'll be showing you here may not be valid when PHP5 reaches a full version. The complete code is available as a ZIP at the end of this article.

SQLite

To get the ball rolling, the first big news is that PHP5 comes with its own, embedded database engine called SQLite, which you can begin to use the moment PHP is installed.

Stunned?

SQLite is a lightweight, public domain database engine written in C, that was developed by D. Richard Hipp of Hipp, Wyrick & Company, Inc., and is freely available from http://www.hwaci.com/sw/sqlite/.

For some of the reasoning behind the development team’s decision to embed it at the heart of PHP, try this post.

SQLite is not intended as a replacement for existing database servers such as MySQL and Postgresql.

If you thought MySQL was rudimentary in terms of what it offers (e.g. lack of triggers, stored procedures etc.), SQLite manages to make it look feature-rich. What SQLite has going for it is speed and the capability to store data in both files and memory. Benchmarks suggest it's roughly twice as fast as MySQL for common operations, and MySQL certainly isn't slow.

Some of the tasks to which I might consider putting SQlite might be application configuration information, storing sessions, storing serialized objects between page requests and possibly to store user login information, depending on the design of the application. Whether I'd use it as my primary data store remains to be seen—I haven't seen how SQLite stands up on a live Website with a lot of traffic. John Lim of PHP Everywhere has some interesting thoughts on SQLite.

Here's how you might use SQLite with PHP5. Note that this won't be an extensive tutorial, but is designed to give you a rough idea of how it works. Have a read of the SQLite FAQ, and SQL documentation for more information. There's no official PHP documentation (other than this presentation) available now, but I found the implementation (in terms of PHP functions) conforms to the SQLite C Interface.

<?php  
// Create SQLite Database and Table  
 
// Display some version information  
echo ( 'SQLite Version: '.sqlite_libversion().'<br />');  
echo ( 'SQLite Encoding: '.sqlite_libencoding().'<br />');  
 
// The file path to the database  
$sqliteDb='/www/sitepoint/php5/sitepoint.sqlite';  
 
// Connect to the database (creates the file)  
if ( !$db = sqlite_open($sqliteDb, 0666, $err) )  
   die($err);  
 
// A query to create a table  
$sql = "CREATE TABLE  
           users  
               (  
               id INTEGER PRIMARY KEY,  
               login STRING UNIQUE,  
               password STRING,  
               email STRING  
               )";  
 
// Run the query  
if ( !sqlite_query($sql, $db) )  
   // Die if errors happen, displaying the error message  
   die(sqlite_last_error($db).': '.  
       sqlite_error_string(sqlite_last_error($db)));  
 
echo ( "Database $sqliteDb created successfully" );  
 
// Close the connection  
sqlite_close($db);  
?>  

Script: sqlite_create.php

The above example creates a table "users" in the database file "/www/sitepoint/php5/sitepoint.sqlite" (the file is created automatically if it doesn't exist).

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links