PHP: __toString() and json_encode() not playing well together

后端 未结 5 1936
故里飘歌
故里飘歌 2021-02-07 14:31

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

相关标签:
5条回答
  • 2021-02-07 15:17

    A late answers but might be useful for others with the same problem.

    In PHP < 5.4.0 json_encode doesn't call any method from the object. That is valid for getIterator, __serialize, etc...

    In PHP > v5.4.0, however, a new interface was introduced, called JsonSerializable.

    It basically controls the behaviour of the object when json_encode is called on that object.


    Example:

    class A implements JsonSerializable
    {
        protected $a = array();
    
        public function __construct()
        {
            $this->a = array( new B, new B );
        }
    
        public function jsonSerialize()
        {
            return $this->a;
        }
    }
    
    class B implements JsonSerializable
    {
        protected $b = array( 'foo' => 'bar' );
    
        public function jsonSerialize()
        {
            return $this->b;
        }
    }
    
    
    $foo = new A();
    
    $json = json_encode($foo);
    
    var_dump($json);
    

    Outputs:

    string(29) "[{"foo":"bar"},{"foo":"bar"}]"

    0 讨论(0)
  • 2021-02-07 15:23

    In PHP > v5.4.0 you can implement the interface called JsonSerializable as described in the answer by Tivie.

    For those of us using PHP < 5.4.0 you can use a solution which employs get_object_vars() from within the object itself and then feeds those to json_encode(). That is what I have done in the following example, using the __toString() method, so that when I cast the object as a string, I get a JSON encoded representation.

    Also included is an implementation of the IteratorAggregate interface, with its getIterator() method, so that we can iterate over the object properties as if they were an array.

    <?php
    class TestObject implements IteratorAggregate {
        
      public $public = "foo";
      protected $protected = "bar";
      private $private = 1;
      private $privateList = array("foo", "bar", "baz" => TRUE);
      
      /**
       * Retrieve the object as a JSON serialized string
       *
       * @return string
       */
      public function __toString() {
        $properties = $this->getAllProperties();
    
        $json = json_encode(
          $properties,
          JSON_FORCE_OBJECT | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
        );
    
        return $json;
      }
    
      /**
       * Retrieve an external iterator
       *
       * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
       * @return \Traversable
       *  An instance of an object implementing \Traversable
       */
      public function getIterator() {
        $properties = $this->getAllProperties();
        $iterator = new \ArrayIterator($properties);
    
        return $iterator;
      }
    
      /**
       * Get all the properties of the object
       *
       * @return array
       */
      private function getAllProperties() {
        $all_properties = get_object_vars($this);
    
        $properties = array();
        while (list ($full_name, $value) = each($all_properties)) {
          $full_name_components = explode("\0", $full_name);
          $property_name = array_pop($full_name_components);
          if ($property_name && isset($value)) $properties[$property_name] = $value;
        }
    
        return $properties;
      }
    
    }
    
    $o = new TestObject();
    
    print "JSON STRING". PHP_EOL;
    print "------" . PHP_EOL;
    print strval($o) . PHP_EOL;
    print PHP_EOL;
    
    print "ITERATE PROPERTIES" . PHP_EOL;
    print "-------" . PHP_EOL;
    foreach ($o as $key => $val) print "$key -> $val" . PHP_EOL;
    print PHP_EOL;
    
    ?>
    

    This code produces the following output:

    JSON STRING
    ------
    {"public":"foo","protected":"bar","private":1,"privateList":{"0":"foo","1":"bar","baz":true}}
    
    ITERATE PROPERTIES
    -------
    public -> foo
    protected -> bar
    private -> 1
    privateList -> Array
    
    0 讨论(0)
  • 2021-02-07 15:25

    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() );
    
    0 讨论(0)
  • 2021-02-07 15:32

    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.

    0 讨论(0)
  • 2021-02-07 15:32

    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).

    0 讨论(0)
提交回复
热议问题