PHP class instance to JSON

后端 未结 5 2174
庸人自扰
庸人自扰 2020-12-08 09:28

I\'m trying echo the contents of an object in a JSON format. I\'m quite unexperienced with PHP and I was wondering if there is a predefined function to do this (like json_en

5条回答
  •  孤城傲影
    2020-12-08 09:58

    In Linux, the following will write the value of a given class entry in a file ~/.config/scriptname/scriptname.conf, create the file if it doesn't exist, and otherwise read and set back the class value at loading:

    /* Example class */
    class flag {
      static $COLORSET = ["\033[34;1m","\033[31;1m"];
    }
    /* Retrieve and set back values, otherwise create config file with the defined value --------------------------------------------------*/
    if (!is_file($_SERVER["HOME"]."/.config/".$_SERVER["SCRIPT_NAME"]."/".$_SERVER["SCRIPT_NAME"].".conf")){
        @mkdir($_SERVER["HOME"]."/.config/".$_SERVER["SCRIPT_NAME"]);
        @file_put_contents($_SERVER["HOME"]."/.config/".$_SERVER["SCRIPT_NAME"]."/".$_SERVER["SCRIPT_NAME"].".conf",json_encode(["COLORSET"=>flag::$COLORSET]));
    } else {
        flag::$COLORSET = json_decode(file_get_contents($_SERVER["HOME"]."/.config/".$_SERVER["SCRIPT_NAME"]."/".$_SERVER["SCRIPT_NAME"].".conf"), true)["COLORSET"];       
    }
    

提交回复
热议问题