How can I create Object in PHP?

11,208

Solution 1

Here is a solution

$newObject = new stdClass;

$newObject->ERROR_CODE = 0;
$newObject->M_USER = new stdClass;
$newObject->M_USER->CREATE_DATE = "2018-05-09 13:57:49";

And so on.

Solution 2

<?php
#Just convert it into a JSON string and decode it 
$your_json =  '{
    "ERROR_CODE": 0,
    "M_USER": {
        "CREATE_DATE": "2018-05-09 13:57:49",
        "CREATE_USER_ID": "t1074567",
        "FACE_PICTURE_FILE_PATH": "<null>",
        "MAIL_ADDRESS": "[email protected]",
        "NATIVE_LANGUAGE_CD": 102,
        "REQ_LANGUAGE_CD": 102,
        "TERMINAL_ID": "C71B456F-EA16-4734-8C9B-00B0856143DA",
        "TERMINAL_TYPE": 1,
        "TOTAL_GRADE": 0,
        "TRANSLATABLE_FLG": 1,
        "UPDATE_DATE": "2018-05-09 13:57:49",
        "UPDATE_USER_ID": "tdu1074567",
        "USER_ID": "tdu1074567",
        "USER_NAME": "Testing",
        "USER_PWD": "testing123",
        "VALID_FLG": 1
    },
    "TRANS_LANGUAGE": [
        {
            "C": 102,
            "L": 10203
        },
        {
            "C": 101,
            "L": 10101
        }
    ]
}';

$object = json_decode($your_json);

print_r($object);
Share:
11,208
Kyaw Zin Wai
Author by

Kyaw Zin Wai

I'm now working as Software Engineer on Japan Company. At first I started with Laravel for 3 months. And then CI for 2 weeks and now I'm handling both Laravel and iOS. Sometimes, i felt brain conflict but that makes me happy by coding more than relax times. I love codes.

Updated on July 19, 2022

Comments

  • Kyaw Zin Wai
    Kyaw Zin Wai almost 2 years

    I want to know how can I create object like below example. Please help me to create object. I searched in google but I didn't get what I want. I'm noob.

    {
        "ERROR_CODE" = 0;
        "M_USER" = {
            "CREATE_DATE" = "2018-05-09 13:57:49";
            "CREATE_USER_ID" = t1074567;
            "FACE_PICTURE_FILE_PATH" = "<null>";
            "MAIL_ADDRESS" = "[email protected]";
            "NATIVE_LANGUAGE_CD" = 102;
            "REQ_LANGUAGE_CD" = 102;
            "TERMINAL_ID" = "C71B456F-EA16-4734-8C9B-00B0856143DA";
            "TERMINAL_TYPE" = 1;
            "TOTAL_GRADE" = 0;
            "TRANSLATABLE_FLG" = 1;
            "UPDATE_DATE" = "2018-05-09 13:57:49";
            "UPDATE_USER_ID" = tdu1074567;
            "USER_ID" = tdu1074567;
            "USER_NAME" = Testing;
            "USER_PWD" = testing123;
            "VALID_FLG" = 1;
        };
        "TRANS_LANGUAGE" = (
            {
                C = 102;
                L = 10203;
            },
            {
                C = 101;
                L = 10101;
            }
        );
    }  
    
  • Kyaw Zin Wai
    Kyaw Zin Wai almost 6 years
    Thakns alot bro. It's worked.
  • McAuley
    McAuley over 3 years
    I really think the answers with '$newObject = new stdClass' are simpler and more to-the-point in answering this question. Your conversion using a json structure, then using json_decode results in a STRING of json notation. Seems simpler just to use the stdClass.
  • Melvin More
    Melvin More over 3 years
    It's an alternative if you have POST or GET or a String and want to convert into a class.