0

Problem statement:

I have to pass a php object from one php application to another using json. In my destination application, I am not able to access my member functions after json_decode.

Here is the sample script:

<?php
class TestScope{
        private $privateVar;
        function __construct(){
                $this->privateVar="private";
        }
        function getPrivateVar(){
                return $this->privateVar;
        }
}
$testScope = new TestScope();
$encode = json_encode($testScope);
$decode = json_decode($encode);
print_r($decode->getPrivateVar());
?>

After executing the script, I am getting below error:

PHP Fatal error:  Call to undefined method stdClass::getPrivateVar()

What is the possible solution to prevent this error?

Tanu Gupta
  • 602
  • 1
  • 11
  • 26

1 Answers1

0

What worked for me, was serializing and un-serializing the object(s) before putting them in cookies, sessions or databases.

I suppose it will work for this problem aswell: http://php.net/manual/en/language.oop5.serialization.php

Xyv
  • 739
  • 7
  • 15