1 | <?php |
||
9 | abstract class BitrixModel extends BaseBitrixModel |
||
10 | { |
||
11 | /** |
||
12 | * Bitrix entity object. |
||
13 | * |
||
14 | * @var object |
||
15 | */ |
||
16 | public static $bxObject; |
||
17 | |||
18 | /** |
||
19 | * Corresponding object class name. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected static $objectClass = ''; |
||
24 | |||
25 | /** |
||
26 | * Fetch method and parameters. |
||
27 | * |
||
28 | * @var array|string |
||
29 | */ |
||
30 | public static $fetchUsing = [ |
||
31 | 'method' => 'Fetch', |
||
32 | 'params' => [], |
||
33 | ]; |
||
34 | |||
35 | /** |
||
36 | * Constructor. |
||
37 | * |
||
38 | * @param $id |
||
39 | * @param $fields |
||
40 | */ |
||
41 | public function __construct($id = null, $fields = null) |
||
49 | |||
50 | /** |
||
51 | * Activate model. |
||
52 | * |
||
53 | * @return bool |
||
54 | */ |
||
55 | public function activate() |
||
61 | |||
62 | /** |
||
63 | * Deactivate model. |
||
64 | * |
||
65 | * @return bool |
||
66 | */ |
||
67 | public function deactivate() |
||
73 | |||
74 | /** |
||
75 | * Internal part of create to avoid problems with static and inheritance |
||
76 | * |
||
77 | * @param $fields |
||
78 | * |
||
79 | * @throws ExceptionFromBitrix |
||
80 | * |
||
81 | * @return static|bool |
||
82 | */ |
||
83 | protected static function internalCreate($fields) |
||
104 | |||
105 | /** |
||
106 | * Delete model. |
||
107 | * |
||
108 | * @return bool |
||
109 | * @throws ExceptionFromBitrix |
||
110 | */ |
||
111 | public function delete() |
||
125 | |||
126 | /** |
||
127 | * Save model to database. |
||
128 | * |
||
129 | * @param array $selectedFields save only these fields instead of all. |
||
130 | * @return bool |
||
131 | * @throws ExceptionFromBitrix |
||
132 | */ |
||
133 | public function save($selectedFields = []) |
||
154 | |||
155 | /** |
||
156 | * Scope to get only active items. |
||
157 | * |
||
158 | * @param BaseQuery $query |
||
159 | * |
||
160 | * @return BaseQuery |
||
161 | */ |
||
162 | public function scopeActive($query) |
||
168 | |||
169 | /** |
||
170 | * Determine whether the field should be stopped from passing to "update". |
||
171 | * |
||
172 | * @param string $field |
||
173 | * @param mixed $value |
||
174 | * @param array $selectedFields |
||
175 | * |
||
176 | * @return bool |
||
177 | */ |
||
178 | protected function fieldShouldNotBeSaved($field, $value, $selectedFields) |
||
193 | |||
194 | /** |
||
195 | * Instantiate bitrix entity object. |
||
196 | * |
||
197 | * @throws LogicException |
||
198 | * |
||
199 | * @return object |
||
200 | */ |
||
201 | public static function instantiateObject() |
||
213 | |||
214 | /** |
||
215 | * Destroy bitrix entity object. |
||
216 | * |
||
217 | * @return void |
||
218 | */ |
||
219 | public static function destroyObject() |
||
223 | |||
224 | /** |
||
225 | * Set eventErrors field on error. |
||
226 | * |
||
227 | * @param bool $result |
||
228 | * @param object $bxObject |
||
229 | */ |
||
230 | protected function setEventErrorsOnFail($result, $bxObject) |
||
236 | |||
237 | /** |
||
238 | * Throw bitrix exception on fail |
||
239 | * |
||
240 | * @param bool $result |
||
241 | * @param object $bxObject |
||
242 | * @throws ExceptionFromBitrix |
||
243 | */ |
||
244 | protected function throwExceptionOnFail($result, $bxObject) |
||
250 | } |
||
251 |
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: