Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...

When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

JSON Expressions

Javascript makes heavy use of anonymnous function callbacks, which can be saved within JSON object variables. Still they only work if not returned inside double qoutes, which Zend_Json naturally does. With the Expression support for Zend_Json support you can encode JSON objects with valid javascript callbacks. This works for both json_encode() or the internal encoder.

A javascript callback is represented using the Zend_Json_Expr object. It implements the value object pattern and is immutable. You can set the javascript expression as the first constructor argument. By default Zend_Json::encode does not encode javascript callbacks, you have to pass the option enableJsonExprFinder and set it to TRUE into the encode() function. If enabled the expression support works for all nested expressions in large object structures. A usage example would look like:

= array(
'onClick' => new Zend_Json_Expr('function() {'
'alert("I am a valid javascript callback '
'created by Zend_Json"); }'),
'other' => 'no expression',
$jsonObjectWithExpression Zend_Json::encode(
'enableJsonExprFinder' => true)

Zend Framework