2011-06-17 14 views
2

添付のコードを参照してください。何らかの理由でjson_encode()が空の文字列を返しています。
私はすべてのアイデア
(PHP 5.3を使用して)
など、私は$videoがnullではなかったことを確認し、ブレークポイントを使用して$jv = Video::ConvertToJson($video);
を使用してそれを呼び出しますか?
THXjson_encode()で空の文字列を取得

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; 

    } 
} 
+0

'ConvertToJson(models \ TfYoutubevideos $ tfv){'←これは私には奇妙に見えます。 'models \ TfYoutubevideos'を削除するとどうなりますか? – knittl

+0

@knittlこれは型指定であり、型は名前空間にあります。これは奇妙に見えますが、それらはPHP 5.3の新機能です。 – phihag

+0

@phihag:ok、私はそれを知らなかった – knittl

答えて

5

json_encodeはプライベート(または保護)メンバ変数をシリアル化しません。オブジェクトの状態を一時配列にコピーするか、メンバ変数をpublicとして宣言して、それを緩和します。

+1

うん、それだった。ありがとう – Cooter

+0

"あなたのオブジェクトの状態を一時的な配列にコピーする" - どうか教えてください。 –

+0

'return json_encode([" ID "=> $ tfv-> getId()、 'タイトル' => $ tfv-> getVideotitle()]);'のような 'ConvertToJson'を実装します。 – phihag

関連する問題