MySQL connections and PHP forked processes

It is possible to fork a process with PHP and have one or more child processes running. If a connection has already been established to a MySQL database and the child processes run queries it’s quite likely a "MySQL server has gone away" or a "Lost connection to MySQL server during query" type error will occur.

Testing if a PHP include file was included

PHP’s include() and include_once() functions can return values to the calling script which can be useful for testing to see if the file was actually included, for example if a configuration file whose filename is based on the website’s domain name was included. If no return value is set then it will return integer 1 if included, or boolean false if not.

Categories PHP

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