1 | <?php |
||
4 | trait SearchableTrait |
||
5 | { |
||
6 | |||
7 | /** |
||
8 | * Searches the internal array for the given value |
||
9 | * and returns the first corresponding key if successful. |
||
10 | * |
||
11 | * If the value can not be found, NULL is returned. |
||
12 | * |
||
13 | * This method uses PHP's array_search: |
||
14 | * @see http://php.net/manual/en/function.array-search.php |
||
15 | * |
||
16 | * @param float $value |
||
17 | * @return int|null |
||
18 | */ |
||
19 | 27 | public function search( float $value ) : ?int |
|
24 | |||
25 | } |
||
26 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.