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?

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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?
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.
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!
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.
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
|

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.