1 | <?php |
||
12 | abstract class Chart |
||
13 | { |
||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $elementID; |
||
18 | |||
19 | /** |
||
20 | * @var Data |
||
21 | */ |
||
22 | protected $data; |
||
23 | |||
24 | /** |
||
25 | * @var ChartOptionsDraw |
||
26 | */ |
||
27 | protected $options; |
||
28 | |||
29 | /** |
||
30 | * @var Events |
||
31 | */ |
||
32 | protected $events; |
||
33 | |||
34 | |||
35 | public function __construct() |
||
36 | { |
||
37 | $this->data = new Data(); |
||
38 | $this->events = new Events($this); |
||
39 | } |
||
40 | |||
41 | |||
42 | /** |
||
43 | * Returns chart's name. |
||
44 | * |
||
45 | * @return string |
||
46 | */ |
||
47 | public function getName() |
||
48 | { |
||
49 | return 'chart' . ucfirst($this->elementID); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Returns data chart's name |
||
54 | * |
||
55 | * @return string |
||
56 | */ |
||
57 | public function getDataName() |
||
58 | { |
||
59 | return 'data'. ucfirst($this->getName()); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Returns options chart's name |
||
64 | * |
||
65 | * @return string |
||
66 | */ |
||
67 | public function getOptionsName() |
||
68 | { |
||
69 | return 'options'. ucfirst($this->getName()); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Returns the chart type. |
||
74 | * |
||
75 | * @return string |
||
76 | */ |
||
77 | abstract protected function getType(); |
||
78 | |||
79 | /** |
||
80 | * Returns the chart package. |
||
81 | * |
||
82 | * @return string |
||
83 | */ |
||
84 | abstract public function getPackage(); |
||
85 | |||
86 | /** |
||
87 | * Returns the instance options. |
||
88 | */ |
||
89 | abstract public function getOptions(); |
||
90 | |||
91 | /** |
||
92 | * Sets the instance Options |
||
93 | * |
||
94 | * @param ChartOptionsDraw $options |
||
95 | * |
||
96 | * @return ChartOptionsDraw |
||
97 | */ |
||
98 | abstract public function setOptions($options); |
||
99 | |||
100 | /** |
||
101 | * Returns the Javascript of the beginning of the chart (Declaration, data and options). |
||
102 | * |
||
103 | * @return string Javascript |
||
104 | * |
||
105 | * @throws GoogleChartsException |
||
106 | */ |
||
107 | public function startDraw() |
||
130 | |||
131 | /** |
||
132 | * Returns the Javascript of the end of the chart (Events and drawing). |
||
133 | * |
||
134 | * @return string |
||
135 | */ |
||
136 | public function endDraw() |
||
141 | |||
142 | /** |
||
143 | * @param string $elementID |
||
144 | * |
||
145 | * @return $this |
||
146 | */ |
||
147 | public function setElementID($elementID) |
||
153 | |||
154 | /** |
||
155 | * @return string |
||
156 | */ |
||
157 | public function getElementID() |
||
161 | |||
162 | /** |
||
163 | * @return Data |
||
164 | */ |
||
165 | public function getData() |
||
169 | |||
170 | /** |
||
171 | * @return Events |
||
172 | */ |
||
173 | public function getEvents() |
||
177 | } |
||
178 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: