1 | <?php |
||
22 | class File |
||
23 | { |
||
24 | /** |
||
25 | * Verify if a file exists |
||
26 | * |
||
27 | * @param string $pFilename Filename |
||
28 | * @return bool |
||
29 | */ |
||
30 | 2 | public static function fileExists($pFilename) |
|
31 | { |
||
32 | // Sick construction, but it seems that |
||
33 | // file_exists returns strange values when |
||
34 | // doing the original file_exists on ZIP archives... |
||
35 | 2 | if (strtolower(substr($pFilename, 0, 3)) == 'zip') { |
|
36 | // Open ZIP file and verify if the file exists |
||
37 | 2 | $zipFile = substr($pFilename, 6, strpos($pFilename, '#') - 6); |
|
38 | 2 | $archiveFile = substr($pFilename, strpos($pFilename, '#') + 1); |
|
39 | |||
40 | 2 | $zip = new \ZipArchive(); |
|
41 | 2 | if ($zip->open($zipFile) === true) { |
|
42 | 2 | $returnValue = ($zip->getFromName($archiveFile) !== false); |
|
43 | 2 | $zip->close(); |
|
44 | |||
45 | 2 | return $returnValue; |
|
46 | } |
||
47 | |||
48 | 2 | return false; |
|
49 | } |
||
50 | |||
51 | // Regular file_exists |
||
52 | 2 | return file_exists($pFilename); |
|
53 | } |
||
54 | /** |
||
55 | * Returns the content of a file |
||
56 | * |
||
57 | * @param string $pFilename Filename |
||
58 | * @return string |
||
59 | */ |
||
60 | 1 | public static function fileGetContents($pFilename) |
|
81 | |||
82 | /** |
||
83 | * Returns canonicalized absolute pathname, also for ZIP archives |
||
84 | * |
||
85 | * @param string $pFilename |
||
86 | * @return string |
||
87 | */ |
||
88 | 1 | public static function realpath($pFilename) |
|
112 | } |
||
113 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.