1 | <?php |
||
9 | class DriverFactory |
||
10 | { |
||
11 | /** |
||
12 | * Return the driver for your system |
||
13 | * |
||
14 | * It accepts a PECL connection, a PhpAmqpLib connection or an array following |
||
15 | * the following schema : |
||
16 | * [ |
||
17 | * 'host' => '<hostValue>', |
||
18 | * 'port' => '<portValue>', |
||
19 | * 'user' => '<userValue>', |
||
20 | * 'pwd' => '<pwdValue>' |
||
21 | * ] |
||
22 | * |
||
23 | * If you provide an array, the PECL extension has precedence over the PhpAmqpLib. |
||
24 | * |
||
25 | * @param $connection |
||
26 | * |
||
27 | * @return Driver |
||
28 | */ |
||
29 | public static function getDriver($connection) |
||
50 | |||
51 | /** |
||
52 | * @param $connection |
||
53 | * |
||
54 | * @return \AMQPConnection | AMQPLazyConnection |
||
55 | */ |
||
56 | private static function getConnectionFromArray($connection) |
||
64 | |||
65 | /** |
||
66 | * @param string[] $connection |
||
67 | * |
||
68 | * @return \AMQPConnection |
||
69 | * |
||
70 | * @codeCoverageIgnore |
||
71 | */ |
||
72 | protected static function getPeclConnection(array $connection) |
||
82 | |||
83 | /** |
||
84 | * @param string[] $connection |
||
85 | * |
||
86 | * @return AMQPLazyConnection |
||
87 | * |
||
88 | * @codeCoverageIgnore |
||
89 | */ |
||
90 | protected static function getPhpAmqpLibConnection(array $connection) |
||
99 | |||
100 | /** |
||
101 | * @return bool |
||
102 | * |
||
103 | * @codeCoverageIgnore |
||
104 | */ |
||
105 | protected static function peclExtensionIsAvailable() |
||
109 | } |
||
110 |
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.