What best practices should be followed when handling file paths and file existence checks in PHP classes?

When handling file paths and file existence checks in PHP classes, it is important to sanitize user input to prevent directory traversal attacks. Additionally, using built-in PHP functions like realpath() can help normalize file paths and avoid unexpected behavior. Finally, always perform file existence checks before attempting to read or write to a file to avoid errors.

class FileHandler {
    public function readFile($filePath) {
        $filePath = realpath($filePath);

        if ($filePath && file_exists($filePath)) {
            return file_get_contents($filePath);
        } else {
            return false;
        }
    }
}