1 | <?php |
||
33 | class GenericMemberNameLoader implements LoaderInterface |
||
34 | { |
||
35 | |||
36 | /** |
||
37 | * The array with the member values. |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $values; |
||
42 | |||
43 | /** |
||
44 | * Construct that initializes the iterator with the parent iterator instance used to load the enities. |
||
45 | * |
||
46 | * @param \TechDivision\Import\Loaders\LoaderInterface $entityLoader The entity loader instance |
||
47 | * @param string $memberName The name of the entity's member to load the values for |
||
48 | * |
||
49 | * @throws \InvalidArgumentException Is thrown, if one of the loaded entities doesn't contain the member with the given name |
||
50 | */ |
||
51 | public function __construct(LoaderInterface $entityLoader, string $memberName) |
||
66 | |||
67 | /** |
||
68 | * Loads and returns data. |
||
69 | * |
||
70 | * @return \ArrayAccess The array with the data |
||
71 | */ |
||
72 | public function load() |
||
76 | } |
||
77 |
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.