Loading optional functionality

Published on and tagged with cakephp  tip

Last week I introduced an optional feature to the OpenID component: if you want to use that feature you have to place a library in a certain folder. If there is no such library, the feature is not active.

In plain PHP you could implement such a solution in the following way:

if (file_exists('path_to_library_file')) {

In CakePHP, we usually use App::import() to load files. And as this method returns false if it can’t load the specified file, we can rewrite the snippet from above to:

if (App::import('Vendor', 'library', array('file' => 'path_to_library_file'))) {

Not really earth-shattering, but maybe it is helpful for some of you ;)

One comment baked

© daniel hofstetter. Licensed under a Creative Commons License