Php implementation of the returned data formatting class instance, php instance. Php implementation of the returned data formatting instance. php instance this article describes the php implementation of the returned data formatting class and its usage, which is very useful in string processing. Return data formatting instances implemented by php, and php instances
This article describes the php implementation of the returned data formatting class and its usage, which is very useful in string processing. Share it with you for your reference. The specific method is as follows:
The DataReturn. class. php class file is as follows:
<? Php/** return data formatting class * Date: 2011-08-15 * Author: fdipzone */class DataReturn {// class start private $ type; private $ xmlroot; private $ callback; private $ returnData; public function _ construct ($ param = array () {$ this-> type = $ this-> exists ($ param, 'type ')? Strtoupper ($ param ['type']): 'json'; // type JSON, XML, CALLBACK, ARRAY $ this-> xmlroot = $ this-> exists ($ param, 'xmlroot ')? $ Param ['xmlroot']: 'xmlroot'; // xml root dom name $ this-> callback = $ this-> exists ($ param, 'callback ')? $ Param ['callback']: ''; // JS callback function name $ format = array (); $ format ['retcode'] = $ this-> exists ($ param, 'format. retcode ')? $ Param ['format'] ['refercode']: 'refercode'; // retcode corresponding name $ format ['MSG '] = $ this-> exists ($ param, 'format. msg ')? $ Param ['format'] ['MSG ']: 'MSG'; // msg name $ format ['data'] = $ this-> exists ($ param, 'format. data ')? $ Param ['format'] ['data']: 'data'; // the corresponding data name $ result = array (); $ result [$ format ['refercode'] = $ this-> exists ($ param, 'refercode ')? $ Param ['retcode']: 0; $ result [$ format ['MSG '] = $ this-> exists ($ param, 'MSG ')? $ Param ['MSG ']: ''; $ result [$ format ['data'] = $ this-> exists ($ param, 'data ')? $ Param ['data']: ''; $ this-> returnData = $ result;} // output data public function data_return () {ob_clean (); switch ($ this-> type) {case 'json': $ this-> json_return (); break; case 'xml': $ this-> xml_return (); break; case 'callback': $ this-> callback_return (); break; case 'array': $ this-> array_return (); break; default: $ this-> json_return ();} exit ();} // output JSON format data. if the callback parameter exists, return the JSONP format private function json _ Return () {header ('content-type: text/html; charset = utf-8 '); if (empty ($ this-> callback )) {echo json_encode ($ this-> returnData);} else {echo $ this-> callback. '('. json_encode ($ this-> returnData ). ');' ;}}// output data in XML format private function xml_return () {header ('content-type: text/xml; charset = utf-8 '); echo $ this-> xml_encode ($ this-> returnData, $ this-> xmlroot);} // output JSON data and call the callback method private function callback_r Eturn () {header ('content-type: text/html; charset = utf-8 '); $ this-> callback = empty ($ this-> callback )? 'Callback': $ this-> callback; echo"