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));
6
  • 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 Commented Jul 12, 2021 at 16:02
  • added the used code Commented Jul 12, 2021 at 16:06
  • ..added the used code... There is no attempt calling json_encode Commented Jul 12, 2021 at 16:07
  • done, added that line Commented Jul 12, 2021 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 Commented Jul 12, 2021 at 16:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.