Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create nested PHP value object? (class)

Tags:

arrays

php

class

I would like to create the following using class syntax:

$resp = new stdclass;

$resp->CategoryListResp->category[0]->categoryId = 1;
$resp->CategoryListResp->category[0]->categoryName = "Spel"; 
$resp->CategoryListResp->category[0]->iconUri = "PictoSpel.png";

$resp->CategoryListResp->category[1]->categoryId = 2;
$resp->CategoryListResp->category[1]->categoryName = "Transport";
$resp->CategoryListResp->category[1]->iconUri = "PictoTransport.png";

Should be easy but I cannot find the syntax for this. I will later output $resp in json format. I am aware I can also use arrays for this...

The json output shall be:

{"CategoryListResp":{"category":[{"categoryId":1,"categoryName":"Spel","iconUri":"PictoSpel.png"},{"categoryId":2,"categoryName":"Transport","iconUri":"PictoTransport.png"}]}}
like image 788
Henrik Lewander Avatar asked Dec 10 '22 06:12

Henrik Lewander


2 Answers

You can also make your classes more explicit:

    class Category {
        public $categoryId = 0, $categoryName = '', $iconUri = '';
    }

    class Resp {
        public $categoryListResp = null;
        public function __construct() {
            $this->categoryListResp = new CategoryListResp();
        }
    }

    class CategoryListResp {
        public $category = array();
    }

    $resp = new Resp();
    $resp->categoryListResp->category[0]->categoryId = 1; 
    $resp->categoryListResp->category[0]->categoryName = "Spel";  
    $resp->categoryListResp->category[0]->iconUri = "PictoSpel.png"; 
    // etc.

ADDED (based on henq's comment). To fully utilize the class concept you would need to add some methods to the classes. Then you would not use -> for arrays, but call the respective methods. E.g.

class Category {
    public $categoryId = 0, $categoryName = '', $iconUri = '';
    public function __construct($id, $name, $icon) {
        $this->categoryId = $id;
        $this->categoryName = $name;
        $this->iconUri = $icon;
    }
}

class Resp {
    public $categoryListResp = null;
    public function __construct() {
        $this->categoryListResp = new CategoryListResp();
    }
    public function addCategory($index, $id, $name, $icon) {
        $this->categoryListResp->addCategory($index, $id, $name, $icon);
    }
}

class CategoryListResp {
    public $category = array();
    public function addCategory($index, $id, $name, $icon) {
        $this->category[$index] = new Category($id, $name, $icon);
    }
}

$resp = new Resp();        
$resp->addCategory(0, 1, "Spel", "PictoSpel.png");
$resp->addCategory(1, 2, "Transport", "PictoTransport.png");
// etc

You can modify this concept according to your needs.

like image 117
Jiri Kriz Avatar answered Dec 28 '22 23:12

Jiri Kriz


You're almost there already:

$resp = new stdClass();
$resp->CategoryListResp = new stdClass();

$resp->CategoryListResp->category[0]->categoryId = 1;
$resp->CategoryListResp->category[0]->categoryName = "Spel"; 
$resp->CategoryListResp->category[0]->iconUri = "PictoSpel.png";

$resp->CategoryListResp->category[1]->categoryId = 2;
$resp->CategoryListResp->category[1]->categoryName = "Transport";
$resp->CategoryListResp->category[1]->iconUri = "PictoTransport.png";

print_r(json_encode($resp));

/* 
output:
{"CategoryListResp":{"category":[{"categoryId":1,"categoryName":"Spel","iconUri":"PictoSpel.png"},{"categoryId":2,"categoryName":"Transport","iconUri":"PictoTransport.png"}]}}
*/

Just send $resp to json_encode. Your code should work as is, however. It's better design to create class definitions for CategoryListResp and Category, rather than just using stdClass.

like image 20
webbiedave Avatar answered Dec 28 '22 23:12

webbiedave