1

I have a php class like,

<?php

namespace app\test;

class TestObject
{
    private $obj_id;
    private $obj_name;

    public function __construct($obj_id, $obj_name)
    {
        $this->obj_id = $obj_id;
        $this->obj_name = $obj_name;
    }

    public function get_obj_id()
    {
        return $this->obj_id;
    }

    public function get_obj_name()
    {
        return $this->obj_name;
    }
}

And in my index.php I am trying to create a TestObject and json encode it with,

$obj_1 = new TestObject(1, "testname");
echo json_encode($obj_1);

But as output I get only {} Any idea, why it is not showing object fields?

Thudani Hettimulla
  • 754
  • 1
  • 12
  • 32

1 Answers1

2

"why it is not showing object fields"

...because they are marked private. As soon as they are marked public they will be visible to json_encode().

class TestObject
{
    public $obj_id;
    public $obj_name;

    public function __construct($obj_id, $obj_name)
    {
        $this->obj_id = $obj_id;
        $this->obj_name = $obj_name;
    }

    public function get_obj_id()
    {
        return $this->obj_id;
    }

    public function get_obj_name()
    {
        return $this->obj_name;
    }
}

$obj_1 = new TestObject(1, "testname");
echo json_encode($obj_1);

Output:

{"obj_id":1,"obj_name":"testname"}

Demo: http://sandbox.onlinephpfunctions.com/code/528d3b495cd23c9ffbea424556cd042c486c413c

Alternatively if you need to keep the properties private and still JSON-encode them, there are various techniques you can employ - see PHP json_encode class private members for details.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Oh great, Yes, it worked. I am from a Java background and always makes class variables private. Is it common practice in PHP to make class variables public? – Thudani Hettimulla Feb 28 '20 at 23:47
  • No more than in Java or any other language. It's not a question of specific practice, in any language. It's a principle of object-oriented programming. There are reasons for making properties private (so that outside callers cannot see them or modify them) and reasons for making them public (so outside callers can see them, to obtain the state of the clas and potentially modify them). Those reasons apply no matter which language you use. – ADyson Feb 28 '20 at 23:49
  • But it just so happens that the json_encode() function can only read from the public properties of a class - probably because it's expected that the only information you would want to export to JSON and share with others would be the information from the class which is public already! – ADyson Feb 28 '20 at 23:50
  • Yes, I understand it is a practice from OOP. Anyway got your point on json_encode(). Thank you! – Thudani Hettimulla Feb 28 '20 at 23:54
  • 1
    No problem. P.S. I modified my answer slightly to add more info, because there are previous questions on this site which show ways you can manage to json-encode private properties of a class, if that is actually necessary in your situation. I added a link to one which has quite a few ideas in the answers. – ADyson Feb 28 '20 at 23:59
  • 2
    If you have followed this and made your variables public, there is no need for your two getter functions, just access your properties via ->propertyName – iJamesPHP2 Feb 29 '20 at 01:47