views:

936

answers:

3

I've run into an odd problem and I'm not sure how to fix it. I have several classes that are all PHP implementations of JSON objects. Here' an illustration of the issue

class A
{
    protected $a;

    public function __construct()
    {
     $this->a = array( new B, new B );
    }

    public function __toString()
    {
     return json_encode( $this->a );
    }
}

class B
{
    protected $b = array( 'foo' => 'bar' );

    public function __toString()
    {
     return json_encode( $this->b );
    }
}

$a = new A();

echo $a;

The output from this is

[{},{}]

When the desired output is

[{"foo":"bar"},{"foo":"bar"}]

The problem is that I was relying on the __toString() hook to do my work for me. But it can't, because the serialize that json_encode() uses won't call __toString(). When it encounters a nested object it simply serializes public properties only.

So, the question then become this: Is there a way I can develop a managed interface to JSON classes that both lets me use setters and getters for properties, but also allows me to get the JSON serialization behavior I desire?

If that's not clear, here's an example of an implementation that won't work, since the __set() hook is only called for the initial assignment

class a
{
    public function __set( $prop, $value )
    {
     echo __METHOD__, PHP_EOL;
     $this->$prop = $value;
    }

    public function __toString()
    {
     return json_encode( $this );
    }
}

$a = new a;
$a->foo = 'bar';
$a->foo = 'baz';

echo $a;

I suppose I could also do something like this

class a
{
    public $foo;

    public function setFoo( $value )
    {
     $this->foo = $value;
    }

    public function __toString()
    {
     return json_encode( $this );
    }
}

$a = new a;
$a->setFoo( 'bar' );

echo $a;

But then I would have to rely on the diligence of the other developers to use the setters - I can't force adherence programmtically with this solution.

---> EDIT <---

Now with a test of Rob Elsner's response

<?php

class a implements IteratorAggregate 
{
    public $foo = 'bar';
    protected $bar = 'baz';

    public function getIterator()
    {
     echo __METHOD__;
    }
}

echo json_encode( new a );

When you execute this, you can see that the getIterator() method isn't ever invoked.

+2  A: 

Isn't your answer in the PHP docs for json_encode?

For anyone who has run into the problem of private properties not being added, you can simply implement the IteratorAggregate interface with the getIterator() method. Add the properties you want to be included in the output into an array in the getIterator() method and return it.

Rob Elsner
But that doesn't work, or I'm doing it wrong. I edited my question to include this example.
Peter Bailey
Alternatively, can you create public variables as these properties, and override __get on that public variable to return the private value, and __set on that variable name to throw an exception?
Rob Elsner
Yes, looking at the PHP source and some other comments, I would recommend you declare public properties, unset them in the constructor, and then override __get to return the private data for that variable name.
Rob Elsner
That won't work either, because then json_encode won't find the variables since they aren't public anymore.
Peter Bailey
A: 

Even if your protected variable was public instead of protected, you won't have the desired input since this will output the entire object like this:

[{"b":{"foo":"bar"}},{"b":{"foo":"bar"}}]

Instead of:

[{"foo":"bar"},{"foo":"bar"}]

It will most likely defeat your purpose, but i'm more inclined to convert to json in the original class with a default getter and calling for the values directly

class B
{
    protected $b = array( 'foo' => 'bar' );

    public function __get($name)
    {
     return json_encode( $this->$name );
    }
}

Then you could do with them whatever you desire, even nesting the values in an additional array like your class A does, but using json_decode.. it still feels somewhat dirty, but works.

class A
{
    protected $a;

    public function __construct()
    {
        $b1 = new B;
        $b2 = new B;
        $this->a = array( json_decode($b1->b), json_decode($b2->b) );
    }

    public function __toString()
    {
        return json_encode( $this->a );
    }
}

In the documentation there are some responses to this problem (even if i don't like most of them, serializing + stripping the properties makes me feel dirty).

pablasso
A: 

You're right the __toString() for the class B is not being called, because there is no reason to. So to call it, you can use a cast

class A
{
    protected $a;

    public function __construct()
    {
        $this->a = array( (string)new B, (string)new B );
    }

    public function __toString()
    {
        return json_encode( $this->a );
    }
}

Note: the (string) cast before the new B's ... this will call the _toString() method of the B class, but it won't get you what you want, because you will run into the classic "double encoding" problems, because the array is encoded in the B class _toString() method, and it will be encoded again in the A class _toString() method.

So there is a choice of decoding the result after the cast, ie:

 $this->a = array( json_decode((string)new B), json_decode((string)new B) );

or you're going to need to get the array, by creating a toArray() method in the B class that returns the straight array. Which will add some code to the line above because you can't use a PHP constructor directly (you can't do a new B()->toArray(); ) So you could have something like:

$b1 = new B;
$b2 = new B;
$this->a = array( $b1->toArray(), $b2->toArray() );
null