Populate defaults dynamically with SilverStripe

SilverStripe allows default values to be set for fields on data objects using the static $defaults property but this cannot be populated with dynamic values, such as using date() or rand(). Instead use the populateDefaults() function.

Style HTML Anchor Titles with jQuery and CSS

The title property of anchor tags in HTML show the text in black on a yellow background by default in most browsers, after mousing over the anchor for a second or two. This tutorial will show you how to style the title with CSS using jQuery and a small amount of Javascript and it will appear as soon as the user mouses over the link.

PHP script to export table creation SQL from MySQL

I was trying to export the structure of a MySQL database using phpMyAdmin but it kept timing out and not showing me the full create script. I think phpMyAdmin uses the information schema to get this information and its on a host with hundreds of databases and tables so querying the information schema runs very slowly. Instead I knocked together a quick PHP script to dump the structure instead and share it here.

Prevent E_DEPRECATED error messages in PHP

PHP 5.3 introduced a new error reporting level E_DEPRECATED which is triggered when deprecated functions and methods are used, such as the old style ereg() regular expression functions. This post shows how to suppress E_DEPRECATED error messages.

Categories PHP

Autoloading with PHP’s SPL library

I read Rafael Dohms’s post “SPL: a hidden gem” a few days ago and learned of a better alternative to PHP’s __autoload function using SPL instead and thought I’d share this here along with some more details about the spl_autoload_register() and spl_autoload_functions() functions.

The useful thing about using spl_autoload_register over __autoload is that it allows the use of multiple autoloaders; __autoload can only have the single autoloading function.

Using a class

Rafael’s example looks like this:

class MyLoader{
 public static function doAutoload($class){
 // autoload process
 }
 }
 
 spl_autoload_register( array('MyLoader', 'doAutoload') );
 

Because an array is passed to the spl_autoload_register() function it knows to call MyLoader::doAutoload($class) when autoloading.

Using a function

It a string is passed instead of an array then it’s a function which is being called: therefore you can either make the autoloader a function or a class. A function example:

function MyFunctionLoader{
 // autoload process
 }
 
 spl_autoload_register('MyFunctionLoader');

If the autoloading function doesn’t exist

When I was testing this out I made the mistake of writing some code passing the function two parameters instead of an array and got the following error:

Fatal error: Uncaught exception 'LogicException' with message 'Function 'MyLoader' not found' in /common/examples/spl-load.php:9
 Stack trace:
 #0 /common/examples/spl-load.php(9): spl_autoload_register('MyLoader', true)
 #1 {main}
 thrown in /common/examples/spl-load.php on line 9

This is because if you pass a string as the first function it looks for a function and not a class. If you want a class to be used make sure you pass array(‘class’, ‘function’) and not ‘class’, ‘function’

Add __autoload()

When using spl_autoload_register the existing __autoload() function is not automatically added to the queue. You need to add it yourself. For example:

if(function_exists("__autoload")) {
     spl_autoload_register("__autoload");
 }
 

Using multiple autoloaders

The following code will register all three of the above autoload functions:

if(function_exists("__autoload")) {
     spl_autoload_register("__autoload");
 }
 spl_autoload_register(array('MyLoader', 'doAutoload'));
 spl_autoload_register('MyFunctionLoader');

If a new class is created and doesn’t already exist in code, then the above functions will be called in the order they are registered and will stop as soon as the class is loaded. In the above example they’ll be called in this order:

  1. __autoload()
  2. MyLoader:doAutoLoad()
  3. MyFunctionLoader()

Showing which functions are registered

Use spl_autoload_functions() to get a list of functions that are registered.

$functions = spl_autoload_functions();
 print_r($functions);
 

With the examples above registered, this will be the output from the array:

Array
 (
 [0] => __autoload
 [1] => Array
 (
 [0] => MyLoader
 [1] => doAutoload
 )
 
 [2] => my_loader
 )
Categories PHP

Clear the bash history

When entering commands in a bash shell each command is stored in the history which is written to the .bash_history file when logging out. This post shows how to clear the history for the current session, and how to clear all commands completely from the history.