Checking if a driver can be loaded

In driver-based packages you should check if the driver exists before loading it. A simple file_exists() does not work since it does not check the include path. fopen()'s third parameter does that, so we use it.

<?php
$driver 
'SomeDriver';
$class  'My_Package_Driver_' $driver;
$file   str_replace('_''/'$class) . '.php';

//check if it exists and can be loaded
if (!@fclose(@fopen($file'r'true))) {
    throw new 
My_Package_Driver_Exception(
        
'Driver ' $driver ' cannot be loaded.'
    
);
}

//continue with including the driver
require_once $file;

//...
?>
Color Representation inside PEAR Packages (Previous) Writing informative release notes (Next)
Last updated: Tue, 29 Jul 2014 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report or add a note.
View this page in:

User Notes:

There are no user contributed notes for this page.