Archive for March, 2007

dirname(__FILE__)

I was just reading the PHP 10.0 Blog when I saw a post called dirname(__FILE__). It’s a call for a new constant that provides the directory of the current file. Personally I think this is a brilliant idea and it shouldn’t be discouraged just because legacy applications will need to use dirname(__FILE__) instead of the new constant. Eventually those versions will disappear. After all, who still writes code that’s backwards compatible with PHP 3?

Let’s get this in now for PHP 6!!

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

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.

Ever since I moved to Ubuntu I haven’t been able to get my XDA II mini to stay connected. The output from dmesg was always the same:

[17179652.184000] usb 2-2: new full speed USB device using uhci_hcd and address 2
[17179652.356000] usb 2-2: configuration #1 chosen from 1 choice
[17179652.460000] usbcore: registered new driver usbserial
[17179652.464000] drivers/usb/serial/usb-serial.c: USB Serial support registered for generic
[17179652.464000] usbcore: registered new driver usbserial_generic
[17179652.464000] drivers/usb/serial/usb-serial.c: USB Serial Driver core
[17179652.472000] drivers/usb/serial/usb-serial.c: USB Serial support registered for PocketPC PDA
[17179652.472000] drivers/usb/serial/ipaq.c: USB PocketPC PDA driver v0.5
[17179652.472000] ipaq 2-2:1.0: PocketPC PDA converter detected
[17179652.472000] usb 2-2: PocketPC PDA converter now attached to ttyUSB0
[17179652.472000] usbcore: registered new driver ipaq

followed about 5 seconds later by:

[17179667.064000] usb 2-2: USB disconnect, address 2
[17179667.064000] ipaq ttyUSB0: PocketPC PDA converter now disconnected from ttyUSB0
[17179667.068000] ipaq 2-2:1.0: device disconnected

The solution turns out to be really simple – TURN YOUR PHONE ON!!

With the phone turned off Linux will detect the phone and set everything up then report the phone as disconnected about 5 seconds later. The solution is to turn your phone on. It doesn’t seem to matter if you turn it on before or after you connect it, just as long as you do it before running synce-serial-start.

Sydney PHP Training

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

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

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.

I’ve been running Evolution 2.8.1 on Ubuntu 6.10 for a few months without any problems. Today Evolution started reporting that it has lost it’s connection to Exchange:

"Can't get message, Lost connection to Evolution Exchange backend process"

After half a day of the Exchange web mail interface driving me crazy I finally fixed the problem. The solution turned out to be simple. After removing the Exchange account from Evolution and quitting it I ran the following commands to remove all evidence of Exchange:

% evolution --force-shutdown
% cd ~/.evolution
% rm -rf `find . -name *exch* -print`

I then restarted Evolution and added the Exchange account again.

Warning: Before removing any files or directories you should back up your entire ~/.evolution directory just in case something goes wrong and you need to restore it.

Here are the slides for the Active Record Design Pattern talk I gave to the Sydney PHP Users Group on March 3, 2007. They are available in PDF and ODF (Open Office) formats.

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'