1 | <?php |
||
35 | abstract class Invitation extends BaseBlameableModel |
||
36 | { |
||
37 | public $hostClass = User::class; |
||
38 | public $idAttribute = false; |
||
39 | public $updatedAtAttribute = false; |
||
40 | /** |
||
41 | * @var array The content field in the data table should be an integer. |
||
42 | */ |
||
43 | public $contentAttributeRule = ['integer']; |
||
44 | /** |
||
45 | * @var bool Whether to allow to send invitation to the same invitee repeatedly. |
||
46 | */ |
||
47 | public $allowRepeated = true; |
||
48 | |||
49 | /** |
||
50 | * @return array |
||
51 | */ |
||
52 | 5 | public function getInviteeRules() |
|
53 | { |
||
54 | $rules = [ |
||
55 | 5 | ['invitee_guid', 'required'], |
|
56 | ['invitee_guid', 'string'], |
||
57 | ]; |
||
58 | 5 | if (!$this->allowRepeated) { |
|
59 | 5 | $rules[] = [ |
|
60 | 5 | [$this->createdByAttribute, $this->contentAttribute, 'invitee_guid'], 'unique', 'targetAttribute' => [ |
|
61 | 5 | $this->createdByAttribute, $this->contentAttribute, 'invitee_guid', |
|
62 | ] |
||
63 | ]; |
||
64 | } |
||
65 | 5 | return $rules; |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * @return array |
||
70 | */ |
||
71 | 5 | public function rules() |
|
75 | |||
76 | /** |
||
77 | * @param User|string $invitee |
||
78 | * @return string |
||
79 | */ |
||
80 | 5 | public function setInvitee($invitee) |
|
84 | |||
85 | /** |
||
86 | * @return User |
||
87 | */ |
||
88 | 3 | public function getInvitee() |
|
93 | |||
94 | /** |
||
95 | * @param User|string|array $invitee |
||
96 | * @return BaseBlameableQuery |
||
97 | */ |
||
98 | 1 | public static function findByInvitee($invitee) |
|
109 | |||
110 | /** |
||
111 | * @return string |
||
112 | */ |
||
113 | 6 | public static function tableName() |
|
117 | } |
||
118 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.