1 | <?php |
||
28 | class FileNode extends File implements FileNodeInterface, FormatAwareInterface, CompressionAwareInterface, EncodingAwareInterface |
||
29 | { |
||
30 | use FormatAwareTrait; |
||
31 | use CompressionAwareTrait; |
||
32 | use EncodingAwareTrait; |
||
33 | |||
34 | /** |
||
35 | * FileNode constructor. |
||
36 | * |
||
37 | * @param FilesystemInterface $filesystem |
||
38 | * @param null|string $path |
||
39 | */ |
||
40 | 112 | public function __construct(FilesystemInterface $filesystem, $path) |
|
45 | |||
46 | /** |
||
47 | * @return string |
||
48 | */ |
||
49 | 14 | public function __toString() |
|
53 | |||
54 | /** |
||
55 | * @return mixed |
||
56 | */ |
||
57 | 10 | public function getDirectory() |
|
61 | |||
62 | /** |
||
63 | * @return string |
||
64 | */ |
||
65 | 18 | public function getFilename() |
|
69 | |||
70 | /** |
||
71 | * Returns the contents of the file as an array. |
||
72 | * |
||
73 | * @return array |
||
74 | */ |
||
75 | 21 | public function getContents() |
|
83 | |||
84 | /** |
||
85 | * @param string|null $newPath |
||
86 | * |
||
87 | * @return static |
||
88 | * @throws CopyFailedException When it is unable to copy the file |
||
89 | */ |
||
90 | 4 | public function copy($newPath = null) |
|
103 | |||
104 | /** |
||
105 | * @return FileSystemWrapperInterface |
||
106 | */ |
||
107 | 16 | public function getFilesystem() |
|
111 | |||
112 | /** |
||
113 | * Return a clone of this object |
||
114 | * |
||
115 | * @return static |
||
116 | */ |
||
117 | 57 | public function getClone() |
|
121 | |||
122 | /** |
||
123 | * Clone sub objects |
||
124 | */ |
||
125 | 57 | public function __clone() |
|
131 | } |
||
132 |
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.