- sprintf: documentation ( source)
- file_exists: documentation ( source)
<?php
class Foo
{
public function tryInclude()
{
try {
require $this->getFileName('include');
} catch (Exception $e) {
echo "Catched";
}
}
private function getFileName($action)
{
$file = '/unknown/path/to/file';
if (false === file_exists($file)) {
throw new Exception(sprintf('The file for this %s was not found.', $action));
} else {
return $file;
}
}
}
$instance = new Foo;
$instance->tryInclude();