1 | <?php |
||
21 | trait ObjectTrait |
||
22 | { |
||
23 | use ArrayableTrait; |
||
24 | use BaseTrait { |
||
25 | BaseTrait::fields insteadof ArrayableTrait; |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * Returns property of item by name. |
||
30 | * @param mixed $name |
||
31 | * @return mixed |
||
32 | */ |
||
33 | 20 | public function get($name) |
|
37 | |||
38 | /** |
||
39 | * Sets an item. Silently resets if already exists. |
||
40 | * @param int|string $name |
||
41 | * @param mixed $value the element value |
||
42 | * @param string|array $where where to put, see [[setItem()]] |
||
43 | * @see setItem() |
||
44 | */ |
||
45 | 80 | public function set($name, $value, $where = '') |
|
53 | |||
54 | /** |
||
55 | * Adds an item. Does not touch if already exists. |
||
56 | * @param int|string $name item name |
||
57 | * @param array $value item value |
||
58 | * @param string|array $where where to put, see [[setItem()]] |
||
59 | * @return $this for chaining |
||
60 | * @see setItem() |
||
61 | */ |
||
62 | 20 | public function add($name, $value = null, $where = '') |
|
70 | |||
71 | /** |
||
72 | * Check collection has the item. |
||
73 | * @param string $name item name |
||
74 | * @return bool whether item exist |
||
75 | */ |
||
76 | 49 | public function has($name) |
|
80 | |||
81 | /** |
||
82 | * Delete an item. |
||
83 | * @param $name |
||
84 | */ |
||
85 | 12 | public function delete($name) |
|
89 | |||
90 | /** |
||
91 | * This method is overridden to support accessing items like properties. |
||
92 | * @param string $name item or property name |
||
93 | * @return mixed item of found or the named property value |
||
94 | */ |
||
95 | 28 | public function __get($name) |
|
103 | |||
104 | /** |
||
105 | * This method is overridden to support accessing items like properties. |
||
106 | * @param string $name item or property name |
||
107 | * @param string $value value to be set |
||
108 | * @return mixed item of found or the named property value |
||
109 | */ |
||
110 | 80 | public function __set($name, $value) |
|
114 | |||
115 | /** |
||
116 | * Checks if a property value is null. |
||
117 | * This method overrides the parent implementation by checking if the named item is loaded. |
||
118 | * @param string $name the property name or the event name |
||
119 | * @return bool whether the property value is null |
||
120 | */ |
||
121 | 5 | public function __isset($name) |
|
125 | |||
126 | /** |
||
127 | * Checks if a property value is null. |
||
128 | * This method overrides the parent implementation by checking if the named item is loaded. |
||
129 | * @param string $name the property name or the event name |
||
130 | * @return bool whether the property value is null |
||
131 | */ |
||
132 | 12 | public function __unset($name) |
|
140 | } |
||
141 |
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.