1 | <?php |
||
15 | class Mime { |
||
16 | |||
17 | private static $mime_types; |
||
18 | |||
19 | private static function ensureDataLoaded() { |
||
25 | |||
26 | |||
27 | /** |
||
28 | * Get the MIME type associated with a given extension. |
||
29 | * |
||
30 | * @param string $extension A file extension e.g. pdf. |
||
31 | * |
||
32 | * @return string|null The associated MIME type or null if none found. |
||
33 | */ |
||
34 | public static function getTypeForExtension($extension) { |
||
48 | |||
49 | |||
50 | /** |
||
51 | * Get the extension associated with a given MIME type. |
||
52 | * |
||
53 | * @param string $mime_type A MIME type e.g. application/pdf. |
||
54 | * |
||
55 | * @return string|null The first available associated extension or null if none found. |
||
56 | */ |
||
57 | public static function getExtensionForType($mime_type) { |
||
71 | |||
72 | |||
73 | /** |
||
74 | * Get all the extensions associated with a given MIME type. |
||
75 | * |
||
76 | * @param string $mime_type A MIME type e.g. application/pdf. |
||
77 | * |
||
78 | * @return string[]|null An array of all the associated extensions or null if none found. |
||
79 | */ |
||
80 | public static function getExtensionsForType($mime_type) { |
||
87 | |||
88 | |||
89 | /** |
||
90 | * Check if an extension is known. |
||
91 | * |
||
92 | * @param string $extension An extension e.g. pdf. |
||
93 | * |
||
94 | * @return boolean |
||
95 | */ |
||
96 | public static function hasExtension($extension) { |
||
112 | |||
113 | |||
114 | /** |
||
115 | * Check if a MIME type is known. |
||
116 | * |
||
117 | * @param string $mime_type A MIME type e.g. application/pdf. |
||
118 | * |
||
119 | * @return boolean |
||
120 | */ |
||
121 | public static function hasType($mime_type) { |
||
125 | |||
126 | |||
127 | /** |
||
128 | * Guess the MIME type of a given file, first by checking the extension then by falling back to magic. |
||
129 | * |
||
130 | * @param string $file_path Relative or absolute path to an existing file. |
||
131 | * @param string $reference_name Use this name for detection based on the extension. |
||
132 | * @param string $default Default MIME type. |
||
133 | * |
||
134 | * @return string|null The associated MIME type or the default if none found. |
||
135 | */ |
||
136 | public static function guessType($file_path, $reference_name = null, $default = 'application/octet-stream') { |
||
155 | |||
156 | |||
157 | /** |
||
158 | * Guess the extension of a given file, first by checking the path then by falling back to magic. |
||
159 | * |
||
160 | * @param string $file_path Relative or absolute path to an existing file. |
||
161 | * @param string $reference_name Use this name for detection based on the extension. |
||
162 | * @param string $default Default extension. |
||
163 | * |
||
164 | * @return string|null The associated extension or the default if none found. |
||
165 | */ |
||
166 | public static function guessExtension($file_path, $reference_name = null, $default = 'bin') { |
||
182 | |||
183 | |||
184 | /** |
||
185 | * Get the MIME type of a file using magic. |
||
186 | * |
||
187 | * @param string $file_path Relative or absolute path to an existing file. |
||
188 | * |
||
189 | * @return string|null The associated MIME type or null if no known type was detected. |
||
190 | */ |
||
191 | public static function getMagicType($file_path) { |
||
201 | } |
||
202 |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: