0

So basically I want to have an Object Class with a bunch of setters that I can manipulate and then convert straight to JSON. In the past I was using an already existing JSON as model(json->object->json). Now I want to just have object->json. But at the moment I get an empty array when I try to use json_encode. Also had some trouble with my $index that I'm using because I can have multiple numerical indexes inside "items"

PHP:

<?php


namespace ProjectApi\Models;


use ArrayObject;
use stdClass;

class MagentoInvoice
{

    public $capture = true;
    public $notify = true;

    public function __construct()
    {

    }

    public function setCapture($capture)
    {
        $this->capture = $capture;
    }
    public function getCapture()
    {
        return $this->capture;
    }

    public function setNotify($notify)
    {
        $this->notify = $notify;
    }
    public function getNotify()
    {
        return $this->notify;
    }

    public function setItems($itemsCount)
    {
        $i = 0;
        while($i < $itemsCount)
        {
            $this->items = new stdClass();
            $i++;
        }
    }
    public function getItems()
    {
        return $this->items;
    }

    public function setProductQty($index, $qty)
    {
        $this->items->$index->qty = $qty;
    }
    public function getProductQty($index)
    {
        return $this->items->$index->qty;
    }

    public function setProductOrderItemId($index, $id)
    {
        $this->items->$index->order_item_id = $id;
    }
    public function getProductOrderItemId($index)
    {
        return $this->items->$index->order_item_id;
    }


}

JSON

{"capture":true,"items":[{"order_item_id":123,"qty":1},{"order_item_id":321,"qty":1}],"notify":true}
        $body = new MagentoInvoice();
        $body->setCapture(true);
        $body->setNotify(true);
        if($firstProductId != null && $secondProductId != null)
        {
            $body->setItems(2);
            $body->setProductQty(0, 1);
            $body->setProductOrderItemId(0, $firstProductId);
            $body->setProductQty(1, 1);
            $body->setProductOrderItemId(1, $secondProductId);
        }
print_r(json_encode($body));
Alex Toma
  • 101
  • 1
  • 9
  • _I get an empty array when I try to use json_encode..._ Where is the code that is using `json_ecode?` - Also: I think you want `$this->items->index->order_item_id` instead of `$this->items->$index->order_item_id` – B001ᛦ Jul 12 '21 at 16:02
  • added the used code – Alex Toma Jul 12 '21 at 16:06
  • _..added the used code..._ There is no attempt calling json_encode – B001ᛦ Jul 12 '21 at 16:07
  • done, added that line – Alex Toma Jul 12 '21 at 16:17
  • Are those properties e.g. items etc declared as private in class `MagentoInvoice` ? Also take a look at [using-json-encode-on-objects-in-php-regardless-of-scope](https://stackoverflow.com/questions/4697656/using-json-encode-on-objects-in-php-regardless-of-scope) – B001ᛦ Jul 12 '21 at 16:23
  • I added Public properties and now the first part works. but I still can't create the subobjects "items" when using ->$index-> or ->index->. error: Creating default object from empty value. Not sure how to create and modify subobject when trying to create multiple items subobjects [0]=> and [1]=> – Alex Toma Jul 12 '21 at 17:03

0 Answers0