/********************************************************************* * PHP Convet class to JSON data * description : * Suddenly want to use the class automatically converted to JSON data, such code extensibility is better, * only need to modify the class's properties to achieve the final JSON data output, but encountered in class * Initialization class variables need to be initialized in the constructor function. * * 2017-8-11 Shenzhen Longhua gen Zengjianfeng ************************************************************* *******/First, reference documents:1. Getting Parse error:syntax error, unexpected t_new [closed] https://stackoverflow.com/questions/15806981/getting-parse-error-syntax-error-unexpected-t-newSecond, the test code:<?PHPclassUart { Public$port ="/dev/ttyo0"; Public$value ="OK"; } classContext { Public$uart =NewUart ();; Public$version ="v0.0.1"; } $context=NewContext; $context _json=Json_encode ($context); echo $context _json?>Third, error content: Parse error:syntax error, unexpected'New'(t_new)inch/usr/share/web/time.php on line8iv. Final code:<?PHPclassUart { Public$port ="/dev/ttyo0"; Public$value ="OK"; } classContext { Public$uart; Public$version ="v0.0.1"; Publicfunction __construct () {$ This->uart =NewUart (); }} $context=NewContext; $context _json=Json_encode ($context); echo $context _json?>v. Output result: {"UART":{"Port":"\/dev\/ttyo0","value":"OK"},"version":"v0.0.1"} VI, Reason: you must DoInitializeNewObjectsinchThe __construct function;
PHP Convet class to JSON data