开发者

OOP PHP - JSON-encoding an object

I want to create a simple class that I can use and encode as a json string. My goal is to encode a json object with info about all my photos on the page. Never used object oriented php before, but I gave it a shot:

class Photo
{
public $file;
public $date;

function __construct($filename, $datetime) 
{
    $file = $filename;
    $date = $datetime;
}

}

Lo开发者_Python百科oping through my photos and creating a new instance of the class for each photo:

$photo = new Photo($filename, $date);

2 problems: echo json_encode($photo); shows me that filename and datetime are null. And when I use echo json_encode($photo);, I will only get the last photo printed, right?


You need to use class member by using $this->varName in your class

So you constructor should be as

function __construct($filename, $datetime) 
{
    $this->file = $filename;
    $this->date = $datetime;
}

as @code_burgar said : need to use array

$photo[] = new Photo($filename, $date);


You need to be sure to reference your instance variables with $this. For example:

function __construct($filename, $datetime) 
{
    $this->file = $filename;
    $this->date = $datetime;
}

I'd encourage you to read PHP's OOP Reference


The above answers are correct you need to use $this.

Try this:

class Photos {
   private $photos = array();

   function add_photo($filename, $date) {
      $this->photos[] = array('filename' => $filename, 'date' => $date);
      return $this;
   }

   function get_all() {
      return json_encode($this->photos);
   }
}


$my_photos = new Photos();

$my_photos->add_photo('test.jpg', '2011-01-01');

echo $my_photos->get_all();

Edit: Forgot to mention, you can also chain up the add_photo method:

$my_photos->add_photo('test.jpg', '2011-01-01')->add_photo('test.jpg', '2011-01-01');


Try with:

class Photo
{
 public $file;
 public $date;

 function __construct($filename, $datetime) 
   {
       $this->file = $filename;
       $this->date = $datetime;
   }

}

And if you want to preserve all your photo objects, you can do

$photos[] = new Photo($file,$date);

and then access them using a foreach() loop


The others answered the question to your first problem. To update members of an object, you must use $this in the context of the object. Externally, use the variable referencing the object since your members are public:

class Photo {
   public $file;
   public $date;
}
$photo = new Photo;
$photo->file = 'file';
$photo->date = time();
echo json_encode($photo);

It is not good design practice to expose members of an object publicly, but json_encode() will not add non-public members to the json string, so you need your own encode method to go this route:

class Photo {
   private $file;
   private $date;
   public function __construct($file, $date) {
      $this->file = $file;
      $this->date = $date;
   }
   public function __encode() {
      $json = new stdClass;
      foreach (get_object_vars($this) as $name => $value) {
         $this->$name = $value;
      }
      return json_encode($json);
   }
}

Your second question seems to imply that you may want multiple photos all stored as a Photo object. You can just put multiple Photo objects into an array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜