1 | <?php |
||
30 | final class Argument implements ProjectFactoryStrategy |
||
31 | { |
||
32 | /** |
||
33 | * @var PrettyPrinter |
||
34 | */ |
||
35 | private $valueConverter; |
||
36 | |||
37 | /** |
||
38 | * Initializes the object. |
||
39 | * |
||
40 | * @param PrettyPrinter $prettyPrinter |
||
41 | */ |
||
42 | public function __construct(PrettyPrinter $prettyPrinter) |
||
46 | |||
47 | /** |
||
48 | * Returns true when the strategy is able to handle the object. |
||
49 | * |
||
50 | * @param Param $object object to check. |
||
51 | * @return boolean |
||
52 | */ |
||
53 | 1 | public function matches($object) |
|
57 | |||
58 | /** |
||
59 | * Creates an ArgumentDescriptor out of the given object. |
||
60 | * Since an object might contain other objects that need to be converted the $factory is passed so it can be |
||
61 | * used to create nested Elements. |
||
62 | * |
||
63 | * @param Param $object object to convert to an Element |
||
64 | * @param StrategyContainer $strategies used to convert nested objects. |
||
65 | * @param Context $context of the created object |
||
66 | * @return ArgumentDescriptor |
||
67 | */ |
||
68 | 2 | public function create($object, StrategyContainer $strategies, Context $context = null) |
|
86 | } |
||
87 |
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.