Rich Buggy

...Developer, CTO, Entrepreneur

PHP

...now browsing by category

 

The need for a static version of __call()

Thursday, March 22nd, 2007

Last month I spoke at the Sydney PHP UG about the Active Record design pattern. For the talk I produced an example implementation. While building it the lack of a static version of __call() really stuck out.

Part of the Active Record design pattern is a way to retrieve records where a field matches a given value. Ideally this function would be static and named something like findByXxx(). Without a static version of __call() the you need to choose between implementing a static find() function that takes two parameters or creating an instance of the object so you can take advantage of __call().

I’m sure someone will tell me to just use find($field, $val) but I think that

$user = User::findByUsername('fred');

Looks much cleaner than

$user = User::find('user', 'fred');

or

$user = new User();
$user = $user->findByUsername('fred');

While it’s been 5 years since I last programmed in C I actually feel passionately enough about this to join the PHP internals mailing list and at least investigate what it would take to have this feature added for PHP 5.3. Of course, while this is being added static versions of __get(), __set(), __isset() and __unset() should probably be added too.

Zend Framework 0.9

Wednesday, March 21st, 2007

Zend has released version 0.9 of the Zend Framework. This will probably be the final release before 1.0. If you haven’t had a look at it then you should. Unlike many frameworks it’s flexible enough to allow the developer to structure their applications the way want to.

Sydney PHP Training

Thursday, March 15th, 2007

Considering the number of organisations using PHP I’m surprised at the lack of training options in Australia. The only courses I could find were “How to build a dynamic web page” style courses, often run by community colleges, that were targeted at individuals just starting out in PHP rather than developers. There is nothing available that corporate Australia can send employees to that provides comprehensive PHP training. As a result I’ve decided to develop a PHP training course to fill this gap.

The first course will be run in Sydney sometime in May or June this year. If you’re interested in attending (or sending someone along) then please contact rich @ buggy.id.au so I can send you some information once the details have been finalised. I’m also after expressions of interest from people in Melbourne as I’d like to offer the course there too.

My top 4 PECL extensions

Saturday, March 10th, 2007

Here are my top 4 PECL extensions I couldn’t live without:

1. Xdebug

When you really need to know what’s going on nothing beats Xdebug. As well as debug information this extension helps profile PHP scripts so they can be optimised.

2. APC.

The Alternative PHP Cache (APC) is an opcode cache. It caches the intermediate code that is created after PHP parses your file making the next request faster by removing the parse step. It’s an easy way to get extra speed when you need it.

3. memcache

The memcache extension allows you to work with memcached to store objects in memory. This allows you to decrease the load on your database.

4. PDO

PDO has been part of PHP since 5.1 but sadly some Linux distro’s don’t include it. The extension provides a uniform API for accessing different database backends, something PERL and Python have had for a while.

What PECL extensions can’t you live without?

Using PECL with Ubuntu

Friday, March 9th, 2007

PECL is a repository for PHP extensions. It’s where old extensions go to die and new extensions are developed. In a previous post I gave the commands to install PDO from PECL but forgot to mention that before you can install anything from PECL you need to install the php5-dev package first.

% sudo apt-get install php5-dev

You need to install the dev package because PECL extensions are compiled into PHP. This should tell you two important things:

  1. Be very careful when deciding which extensions to include. While an unstable PEAR package might cause your application to fail a bad PECL extension can cause your web server to crash.
  2. When you upgrade PHP you will probably need to reinstall the PECL extension.

Adding Open Office support to WordPress

Monday, March 5th, 2007

I recently wanted to upload an Open Office presentation to WordPress 2.1 and received the message:

File type does not meet security guidelines. Try another.

It turns out that WordPress limits the types of files you can upload. Fixing it was pretty simple. You just need to look for the function wp_check_filetype() in the wp-includes/functions.php file and add the following.

'odt' => 'application/vnd.oasis.opendocument.text',
'ott' => 'application/vnd.oasis.opendocument.text-template',
'oth' => 'application/vnd.oasis.opendocument.text-web',
'odm' => 'application/vnd.oasis.opendocument.text-master',
'odg' => 'application/vnd.oasis.opendocument.graphics',
'otg' => 'application/vnd.oasis.opendocument.graphics-template',
'odp' => 'application/vnd.oasis.opendocument.presentation',
'otp' => 'application/vnd.oasis.opendocument.presentation-template',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
'ots' => 'application/vnd.oasis.opendocument.spreadsheet-template',
'odc' => 'application/vnd.oasis.opendocument.chart',
'odf' => 'application/vnd.oasis.opendocument.formula',
'odb' => 'application/vnd.oasis.opendocument.database',
'odi' => 'application/vnd.oasis.opendocument.image'

I'm now a ZCE

Tuesday, February 27th, 2007

I passed the Zend PHP 5 Certification exam today. After spending a couple of weeks studying to make sure I passed it I’m thrilled at the result.

Installing PDO for PHP5 on Ubuntu

Monday, February 19th, 2007

For some reason there aren’t any PDO packages for PHP 5 on Ubuntu 6.10. They’re pretty easy to install from PECL but you might need to install the dev version of your database client libraries. Below is what I had to type to install PDO with the MySQL driver.

% sudo apt-get install libmysqlclient15-dev
% sudo pecl install pdo
% sudo pecl install pdo_mysql

You then need add the following to the end of your php.ini file(s). Depending on which version of PHP you installed they’ll be /etc/php5/apache2/php.ini, /etc/php5/cgi/php.ini and /etc/php/cli/php.ini.

extension=pdo.so
extension=pdo_mysql.so

Update: Before you can install any PECL extensions you need to install the php5-dev package. For information see my post about Using PECL with Ubuntu.

Top 6 framework gripes

Saturday, February 17th, 2007

I just read a post by Andrei Zmievski pleading for people to stop releasing their own frameworks. As someone who has my own (unreleased) framework I wanted to answer why don’t I use an existing framework. It’s simple really, typically they’re slow, bloated and love to provide wrappers for PHP functions.

Here are my top 6 framework gripes:

  1. Using their own database abstraction layer.
  2. Using the Active Record design pattern in production applications.
  3. Using a template engine instead of PHP.
  4. Using XML to store configuration information.
  5. Adding code until the framework can be used in every imaginable situation.
  6. Including classes that provide rarely used functionality in the core framework.

Active Record design pattern talk

Friday, February 16th, 2007

At the next Sydney PHP Users Group meeting I’ll be doing a talk on the Active Record design pattern. Design patterns are simply a template for how to solve commonly occurring problems. In fact, you’re probably using them now without even knowing it.

The Active Record design pattern allows you to access a database table using CRUD (Create, Retrieve, Update & Delete) without needing to write any database code. How cool is that!!