Why wrap require autoloader in a is_readable() check? - WebDevStudios/oops-wp GitHub Wiki
In the example code in README.md
the line to require the Composer autoloader is wrapped in a is_readable()
check, and this deserves some explanation.
if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
require_once __DIR__ . '/vendor/autoload.php';
}
Where is Your Autoloader?
If you add OOPS-WP to your project and run composer install
from the root of your project, your dependencies (including the Composer autoloader) will be installed in the vendor
directory in the root of your project.
However, if another project loads your project as a dependency (e.g. you create a plugin using OOP-WP and they use Composer to load your plugin into their project), your dependencies (including the Composer autoloader) will end up in their vendor
directory. In this case, attempting to require ./vendor/autoload.php
from your project will throw a fatal error (because the autoloader will not be there).
By adding the is_readable()
check, we ensure the file exists (and is readable) before requiring it. This check allows us to require the autoloader when the file is local to our project and avoids throwing a fatal error when the autoloader is not local to our project.
Note When a project loads your project as a dependency, the assumption is they will require the autoloader generated for their vendor
directory (so we take no action).