1

I have a complex object in PHP and need to parse it to build a Json String. I found many exmaples i've found here and other sites, but no one worked. The further problem is that my hosting works on PHP 5.2 and i cannot upgrade.

Here is an example of my var_dump($myObj):

object(Park)[4]
private 'idObj' => string '60304' (length=5)
private 'name' => string 'AlphaSurf' (length=9)
private 'address' => 
object(Address)[6]
  private 'idObj' => string '40304' (length=5)
  private 'street' => string 'Champ de la Vigne' (length=17)
  private 'number' => string '7' (length=1)
  private 'zip' => string '1470' (length=4)
  private 'city' => string 'Estavayer-le-Lac' (length=16)
  private 'country' => 
    object(Country)[8]
      private 'idObj' => string '30039' (length=5)
      private 'name' => string 'Switzerland' (length=11)
      private 'flag' => string 'switzerland.gif' (length=15)
  private 'usState' => null
private 'contactInfo' => 
object(ContactInfo)[7]
  private 'idObj' => string '70304' (length=5)
  private 'phone' => string '' (length=0)
  private 'email' => string '' (length=0)
  private 'emailcode' => null
  private 'confirmed' => string '1' (length=1)
  private 'website' => string 'www.alphasurf.ch' (length=16)
  private 'mobile' => string '' (length=0)
  private 'fax' => string '' (length=0)
  private 'newsletter' => string '0' (length=1)
private 'owner' => 
object(User)[9]
  private 'idObj' => string '50001' (length=5)
  private 'username' => string 'emaborsa' (length=8)
  private 'password' => string '5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8' (length=40)
  private 'type' => string 'ADMIN' (length=5)
  private 'state' => string 'ACTIVE' (length=6)
  private 'ip' => string '' (length=0)
  private 'time' => string '0' (length=1)
  private 'address' => null
  private 'contactInfo' => 
    object(ContactInfo)[11]
      private 'idObj' => string '1' (length=1)
      private 'phone' => null
      private 'email' => string 'info@emaborsa.com' (length=17)
      private 'emailcode' => null
      private 'confirmed' => string '1' (length=1)
      private 'website' => null
      private 'mobile' => null
      private 'fax' => null
      private 'newsletter' => string '1' (length=1)
private 'logo' => string 'Champ de la Vigne 71470' (length=23)
private 'xcoord' => string '46856912' (length=8)
private 'ycoord' => string '6846918' (length=7)
private 'state' => string 'HIDDEN' (length=6)
private 'detail' => 
object(ParkDetail)[10]
  private 'idObj' => string '1' (length=1)
  private 'descriptionIT' => string '' (length=0)
  private 'descriptionEN' => string '' (length=0)
  private 'descriptionDE' => string 'xcxcx' (length=5)
  private 'type' => string '' (length=0)
  private 'kickers' => string '0' (length=1)
  private 'boxes' => string '0' (length=1)
  private 'rails' => string '0' (length=1)
  private 'specials' => string '0' (length=1)
  private 'specialsDescriptionIT' => null
  private 'specialsDescriptionEN' => null
  private 'specialsDescriptionDE' => null
  private 'dimension' => string '0' (length=1)
private 'lastPayment' => null

All properties are private but there are public getters and setters.

Emaborsa
  • 2,360
  • 4
  • 28
  • 50

3 Answers3

2

Try this

public function encodeJSON() 
{    
    foreach ($this as $key => $value) 
    { 
         if($value instanceOf(stdClass)){
             $json->$key = $value->encodeJSON();
         }else
             $json->$key = $value; 
    } 
    return json_encode($json);
}

i'm trying to move the private members to a new object that can be written by normal json_encode() and in line 6 i'm calling it recursively foreach parameter if it not a primative type

Majed DH
  • 1,251
  • 18
  • 35
  • ...well, where do i have to put it? – Emaborsa Mar 19 '14 at 14:03
  • into the class you want to echo json of it, use `$this->encodeJSON()` and you have to write it into every object that have private members you want to JSON it – Majed DH Mar 19 '14 at 14:04
  • Does it work if i create an Json class with this method and i extend each class from this new Json class? – Emaborsa Mar 19 '14 at 14:09
  • it worked by adding the below code into each class. Defining a single one inherited by all classes doesn't work. – Emaborsa Mar 19 '14 at 15:00
  • 1
    And you are telling me to read about encapsulation? Your properties are private so if you put this method in a class and extend it won't be able to access the private properties. – YRM Mar 19 '14 at 15:20
  • 1
    @MajedDH would have been nice if you quoted the original answer you pasted yours from. – BMN Mar 19 '14 at 16:48
1

I think you made a design mistake by needing to expose private property values.

But of course there are scenarios where this should be done. As pointed out by PHP Object To JSON format one way of doing this would be trough reflection.

Here is a simple example using php ReflectionClass to achieve what you want:

function getJson($object)
{
    $result = array();
    $refl = new ReflectionClass($object);
    foreach ($refl->getProperties() as $prop) {
        $prop->setAccessible(true);
        $result[$prop->name] = $prop->getValue($object);
    }
    return json_encode($result);
}
Community
  • 1
  • 1
YRM
  • 301
  • 4
  • 13
0

As you're running PHP < 5.4, I'd recommend creating a toArray method in your objects, returning an array containing all the properties (without bothering if they are public, private or protected).

For example :

public class Park {

    private $idObj;
    private $address;

    public function toArray() {

        $toArray = array(
            'idObj' => $this->idObj,
            'address' => $this->address->toArray() // Assuming address is an Address object
        );
    }
}

Do this in all your classes and sub-classes and then you can use :

$park = new Park(/* your values to initialize the object */);
echo json_encode($park->toArray());
BMN
  • 8,253
  • 14
  • 48
  • 80
  • Are you sure that is the best way? I have 25 classes, i would have to do it in each single class. I guess there must be a way to do it through reflection in one single class or something else. – Emaborsa Mar 19 '14 at 14:08
  • Well, with PHP 5.4 came the `JsonSerializable` interface that might have helped but with an earlier version of PHP, it's way more 'complicated' to achieve this. I'd suggest you have a look at this answer : http://stackoverflow.com/a/4697671/831669 and this one : http://stackoverflow.com/a/14796817/831669 – BMN Mar 19 '14 at 14:29
  • I've already seen an example like this, it'd mean that i have to write a proper method for every class... – Emaborsa Mar 19 '14 at 14:30
  • @Emaborsa or upgrade your outdated PHP version. – BMN Mar 19 '14 at 16:50