1

I have this class with three variables, one of them being an array. I have written a method that I'll use inside json_encode().

public function getJSONString(){
     return [
         'id' => $this->id,
         'name' => $this->name,
         'books' => $this->books
     ];
}

books is an array of Book objects. Each Book object also has the same exact method but then with its own variables.

public function getJSONString(){
    return [
        'id' => $this->id,
        'title' => $this->title
    ];
}

When I call print(json_encode($author->getJSONString())) I recieve this:

{"id":"1","name":"name1","books":[{},{}]}

Any idea why books remains empty? Thanks in advance!

2
  • 1
    Probably $this->books is empty. Check if it's empty var_dump($this->books) and check the code that sets it's value may be it is executing after you called getJSONString() Commented Apr 21, 2018 at 18:53
  • 1
    Books are empty because they're empty initially. print_r() your variable and see. Commented Apr 21, 2018 at 18:57

2 Answers 2

2

You will need to use the Books' getJSONString() methods as well. This will make the the books property equal to an array of JSON-encode-able objects.

In the author's getJSONString() method:

public function getJSONString(){
     return [
         'id' => $this->id,
         'name' => $this->name,
         'books' => array_map(function($book) { return $book->getJSONString(); }, $this->books)
     ];
}

I am not fluent in PHP, but you can get the idea from this. It maps a function to get the getJSONString() over the books array.

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

7 Comments

I get the idea of calling getJSONString of every book but I think in this example it's calling the wrong getJSONString method. See the class Book has it's own implementation of the method, and right now I think it keeps calling the getJSONString in the same class where $books is declared?
@Notorious Oops, I made a silly mistake in my first code. I changed it so that it uses $book->getJSONString(). Tell me if it works now.
Works fine now. Appreciate the help alot!
@Notorious Glad I helped!
This is not a good way to do it. Using JsonSerializer is the cleaner way to go. See my answer.
|
0

You should definitely use JsonSerializable interface.

class Book implements JsonSerializable 
{
    public function jsonSerialize() {
        return [
           'id' => $this->id,
           'title' => $this->title
        ];
    }
}

When you call json_encode for an object of this class or anything contains that, php will call your jsonSerialize method. Similar to what you are trying to do, but this is the language specific and right way to do it. Other languages like C# has similar interfaces too.

Comments

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.