How can the finfo class be implemented in an object-oriented manner in PHP?
To implement the finfo class in an object-oriented manner in PHP, you can create a new class that encapsulates the functionality of the finfo class. This new class can have methods to perform file type detection and provide information about files. By using object-oriented programming principles, you can create a more modular and reusable solution for working with file information in PHP.
class FileInformation {
private $finfo;
public function __construct() {
$this->finfo = new finfo(FILEINFO_MIME_TYPE);
}
public function getFileMimeType($filename) {
return $this->finfo->file($filename);
}
public function getFileExtension($filename) {
return pathinfo($filename, PATHINFO_EXTENSION);
}
}
// Example of how to use the FileInformation class
$fileInfo = new FileInformation();
$filename = 'example.txt';
echo 'File MIME type: ' . $fileInfo->getFileMimeType($filename) . PHP_EOL;
echo 'File extension: ' . $fileInfo->getFileExtension($filename) . PHP_EOL;