PHP in Action – Objects, Design, Agility

January 31, 2010 1 comment

Book Review

PHP in Action is a book aimed at people who are comfortable coding procedural or object orientated PHP scripts. It doesn’t teach you what a variable is or what function to use if you want to connect to a database but it does teach you design patterns, best practice techniques and useful information about PHP5′s object system. If you are looking into writing a PHP framework or want to further understand design patterns and objects in PHP then I highly recommend this book. It’s written by prominent members of the PHP community – Dagfinn Reiersol, Marcus Baker and Chris Shiflett and is 525 pages long.

Overview of the book

Chapters 2,3,4 cover the PHP5 class system. The book offers detailed and useful examples of each of the topics as well as plenty of metaphors to help the reader understand exactly what each feature should do and is capable of doing. I found this contrasted greatly with the almost minimal explanations and examples offered by the official documentation on php.net. The following features are covered:

  • Magic Methods – __get(), __set(), __construct(), __autoload()
  • Exception handling
  • Error handling

Using Facebook Connect for your web application

January 13, 2010 3 comments

With the exponential growth of Facebook over the past few years, it’s safe to say that quite a large number of active web users today own and use a Facebook account regularly. Facebook connect, which Facebook launched late 2008, is a set of APIs which allow you to integrate your users’ Facebook profile into your web application. Apart from allowing your visitors to login to your site using their Facebook identity (hence negating the registration process), it also allows you to leverage on the various social features of Facebook. Let’s take a overview of Facebook Connect, and how we can integrate it into our web application.

Facebook APIs

Facebook provides both RESTFUL and Javascript APIs. A great number of client libraries are available for creating your web application, though Facebook provides official support for only a handful of them. We will be using Facebook’s official PHP 5 client and Javascript libraries.

FQL and XFBML

Before we look delve deeper into Facebook connect integration, let’s look at FQL and XFBML – the two essential components of the Facebook connect platform.

FQL (yes, Facebook Query Language!) allows you to query data from Facebook easily using a SQL like…

PHP Namespaces

January 8, 2010 No comments yet

Namespaces were introduced into PHP from version 5.3 onwards. They allow the developer to seperate their code into modules or groups which inturn makes the code easier to read. Namespaces prevent class and function name conflicts, it allows you to have numerous classes with the same name, as long as they are in different namespaces. Namespaces also make the old Zend/PEAR style of class name redundant (Framework_Database_Mysql_Query) because there will no longer be any class name conflicts.

Note: throughout the tutorial we will use the hypothetical files.

/Framework/Database/Mysql/Query.php
/lib/myClass.php
/index.php

The Basics

To declare a namespace you simply use the namespace keyword followed by the name of your namespace. You can specify sub namespaces by using the backslash ( \ ). Best practices state that your namespace path should match the directory path. For example the file /Framework/Database/Mysql/Query.php should have the namespace Framework\Database\Mysql and the name of the class should be Query. You should have one namespace and class per file.

Using Namespaces

In /lib/myClass.php we declare a namespace, lib, and then define a simple class, myClass. In our index.php file we include the class and instantiate it. Because myClass is in the namespace lib, we must refer to…