3

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;
}

}

Looping 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?

5 Answers 5

4

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);
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

2

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');

1 Comment

just a little mistake of ' . Use $my_photos->add_photo('test.jpg', '2011-01-01');
2

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

Comments

1

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.

Comments

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.