PHP json_encode encode a function?

JSON is for passing values around, they are not suitable for passing pieces of code.

JSON is for passing values around, they are not suitable for passing pieces of code. You can, instead, pass a function name or other meaningful value and retrieve the right function to call from it on the JavaScript side.

Alternatively I solve that by returning a string, but this is make the code not "clean" $returnValue = "{'title': 'Title', 'fnCallback': someCallback}"; – brian May 29 '11 at 18:54.

Viola I solved my problem with Zend_JSON encoder $options = array( 'title' => 'Title', 'fnCallback' => new Zend_Json_Expr('someCallback') ); Zend_Json::encode( $options, false, array('enableJsonExprFinder' => true)).

You forgot the comma between 'title' and 'fnCallback.

Oops i'm sorry, but it is not the problem. SomeCallback is illegal in PHP but not in js. – brian May 29 '11 at 18:47 I know, but you did ask why the PHP code is not working.

– dunsmoreb May 29 '11 at 18:48 yeah I supposed JSON_ENCODE can reference the function but i'm wrong see @mario – brian May 29 '11 at 18:51.

Don't confuse JSON for actual, native, Javascript object notation syntax (regardless of the name). Javascript objects can contain function references; JSON cannot.

To make the notice go away in PHP, just write your callback function in quotes: $options = array( 'title' => 'Title', 'fnCallback' => "someCallback"); And then when you receive the JSON in Javascript, you can remap the callback function name to the actual JS function with e.g. : json = $. GetJSON(..); json. FnCallback = windowjson.

FnCallback; // for global callbacks.

Now I know the basic, thank you very much mario – brian May 29 '11 at 19:03.

That is not possible without thinking of a convention and implementing it yourself. Say, you have this JSON '{"title": "Title", "fnCallback": "someCallback" }' Then you could do, on the client side function wireupCallbacks(jsonObject) { if (typeof jsonObject === "object") { for (var prop in jsonObject) { var callbackName = jsonObjectprop; if (/Callback$/. Test(prop) && typeof callbackName === "string") { if (typeof thiscallbackName === "function") { jsonObjectprop = thiscallbackName; } } } } return jsonObject; } and call that in the context of an object that provides your callback functions var someObject = { someCallback: function() { alert("It works!"); } } var jsonObject = {"title": "Title", "fnCallback": "someCallback" }; wireupCallbacks.

Call(someObject, jsonObject); jsonObject.fnCallback(); // alerts "It works! " What's missing: currently the function only looks for properties named "*Callback". There is no fallback to global functions (these would be properties of the window object) there is no recursion (nested objects are not visited) there is no JSON array handling Add these features on your own, none of these should be difficult to implement.

Yeah beautiful, thanks – brian May 29 '11 at 19:04.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions