SLB: Notes – File Loading: Context & Callbacks
By Sol in Lab
Requirements
- Conditional use of callback (based on context)
- Only run callback if associated context is valid
- Global use of callback (regardless of context)
Notes
- Callbacks are always assumed to be internal methods of calling instance object, thus callbacks are defined as a
string
(method name)
'callback-name'
set to $this->callback-name()
when parsed
Methodology (Options)
- Pair context/callback
- Conditional callback
- Defined in
context
property
- Context only:
(string) 'context-name'
- Context + Callback:
(array) ['context-name', 'callback']
- Global callback
- Defined in
callback
property
+
Clearly separated functionality (conditional vs. global)
+
Allows global & conditional callbacks simultaneously (global callback and context/callback pair will be evaluated if set)
-
Ambiguous – Multiple contexts are grouped in an array & context/callback pairs are also grouped in an array
- Issue when single context specified (single context does not currently need to be placed in an array)
- Options
- Mandatory wrapping of contexts in array
context
property is always an array
+
Consistent: No chance for confusion
-
Extra work: must always create array
+
Strict specification: No edge cases
- Context/Callback in separate properties, but matching
- e.g. 1st context matches 1st callback, etc.
context
property: (array) ['context-1', 'context-2', 'context-3']
callback
property: (array) ['callback-1', null, 'callback-3']
- Pairs (context/callback):
context-1/callback-1
, context-2/(no callback)
, context-3/callback-3
?
Global callback?
- callback
property populated with conditional callbacks
?
Context without callback?
- Set
null
at matching index in callback
array
-
Must always keep callbacks/contexts matched up
- Context/Callback pair defined as
string
- Similar to 1 but using delimiter to separate context & callback
- Example:
'context-name|callback-name'
(pipe character is delimiter)
+
Simple to implement: No need to add arrays, etc.
+
Less to type: just delimiter
-
More prone to errors: Since values are not separated by array
?
Less strict specification than 1 good or bad?
-
Must keep track of delimiter character
- Array is specific/consistent, while delimiter character may be changed (error if new delimiter is not propagated throughout code)