2

See attached code. for some reason the json_encode() is returning an empty string.
I call it by using $jv = Video::ConvertToJson($video);
Using breakpoints I verified that $video wasn't null, etc.
(using PHP 5.3)
Any ideas?
thx

class Video
{
    private $ID;
    private $Title;
    private $ViewCount;
    private $Description;
    private $IsEmbeddable;
    private $IsPrivate;

    function __construct($id = 0, $title = '', $viewcount=0, $description='', $isembeddable=1, $isprivate=0){
        $this->ID = $id;
        $this->Title = $title;
        $this->ViewCount = $viewcount;
        $this->Description = $description;
        $this->IsEmbeddable = $isembeddable;
        $this->IsPrivate = $isprivate;
    }
    /**
     *
     * Converts a Tfyoutubevideo into a json object
     * @param models\TfYoutubevideos $tfv
     */
     public static function ConvertToJson(models\TfYoutubevideos $tfv){
        $v = new Video();
        $v->ID = $tfv->getId();
        $v->Title = $tfv->getVideotitle();
        $v->ViewCount = $tfv->getVideoviewcount();
        $v->Description = $tfv->getVideoDescription();
        $v->IsEmbeddable = $tfv->getVideoIsEmbeddable();
        $v->IsPrivate = $tfv->getVideoIsPrivate();
        $vj = json_encode($v);
        return $vj;

     }
}
2
  • ConvertToJson(models\TfYoutubevideos $tfv){ ← this looks strange to me. what if you drop the models\TfYoutubevideos? Commented Jun 17, 2011 at 16:37
  • @knittl It's a type specification, and the type is in a namespace. It looks strange, but that's because those are features new to php 5.3. Commented Jun 17, 2011 at 16:38

1 Answer 1

5

json_encode does not serialize private (or protected) member variables. Either copy your object's state into a temporary array or declare member variables as public to mitigate that.

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

2 Comments

"copy your object's state into a temporary array" - please can you tell me how?
Implement ConvertToJson like return json_encode(["ID" => $tfv->getId(), 'Title' => $tfv->getVideotitle()]);

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.