-
Notifications
You must be signed in to change notification settings - Fork 3
Callbacks
* [#What_are_callbacks What are callbacks] * [#phpQuery_callbacks phpQuery callback system] * [#Callback Callback class] * [#CallbackParam CallbackParam class] * [#CallbackReference CallbackReference class] * [#Scope_Pseudo_Inheritance Scope Pseudo-Inheritance]
Callbacks are functions _called back_ by other functions in proper moment (eg on Ajax request error).
In *JavaScript* this pattern can be very flexible due to Closures support, which can be inline (no code break) and inherits scope (no need to passing params).
- PHP* has only simple support for callbacks so the case is more complicated. That's why phpQuery extends callback support by it's own.
phpQuery uses it's own approach to callbacks. This task is achieved thou *Callback*, *CallbackParam* and *CallbackReference* classes.
Callback class is used for wrapping valid callbacks with params.
As we can see in [#Example_1], new instance of CallbackParam class is used for defining places, where original callback parameter(s) will be placed. Such pattern can be used also without Callback class for some methods.
Methods supporting CallbackParam *without* using Callback class:
* `phpQuery::each()` * `phpQuery::map()` * `pq()->each()` * `pq()->map()`
Finally, CallbackReference can be used when we don't really want a callback, only parameter passed to it. CallbackReference takes first parameter's value and passes it to reference. Thanks to that, we can use *if statement* instead of *callback function*.
There is an easy way to pseudo inherit scope in PHP. Scope means _variables accessible in specified point of code_ (which in other words means _any variable you can use_). It's achieved using compact() and extract() functions.
Look at this modified [#Example_2]. Previous comments were removed.
In the future this functionality will be extended and more methods will support it. Check Issue Tracker entry #48 if you're interested in any way.