tags:

views:

55

answers:

7

I am building a small gallery that will only have 20 or so images in it. I'd like to have this data stored somewhere (even in the PHP file itself) and so am looking at ways of encoding the data as a literal or resource file.

In JavaScript, I would use notation resembling (from memory):

var albums = [
  { name='Album 1', photos=['photo1.jpg', 'photo2.jpg'] },
  { name='Album 2', photos=['photo3.jpg', 'photo4.jpg'] }
]

This is JSON, in essence.

I suppose JavaScript is more dynamic than PHP and so this is not possible. Is there a simple alternative, perhaps using XML and binding to some classes?

I'm a complete PHP novice, so please don't assume any knowledge in your answer.

+1  A: 

If you just have some constant data that you want to store in a PHP file, you can use the same thing as you did in Javascript : just declare a PHP array.


Something like this should do the trick :

$albums = array(
    array(
        'name' => "Album 1", 
        'photos' => array('photo1.jpg', 'photo2.jpg'), 
    ), 
    array(
        'name' => "Album 2", 
        'photos' => array('photo3.jpg', 'photo4.jpg'), 
    ), 
);

And, then, you just can work with the $albums array.


Of course, it's not easy to update, you have to write some valid PHP code -- but if you only have a couple of images, like you said, it should not be that hard to manage.


Another solution would be to store the data in an external file (in XML or JSON, for instance), and use something like simplexml_load_file or json_decode to read it.

But it means a bit more work -- so maybe not that necessary in your case ?

Pascal MARTIN
Thanks Pascal. I found this page http://www.herongyang.com/PHP/Data-Type-Data-Literal-Supported-in-PHP.html that stated `There are no data literals for array data type` and so I didn't look any further! This approach is a bit more verbose than the JSON equivalent, but only slightly and it might work out well. Ideally I'd like to bind to my own types, but I can't find any deserialisation functions that bind to known types.
Drew Noakes
@Skilldrick: `[1, 2, 3]` is not valid PHP code, neither in initalization, nor elsewhere.
soulmerge
+1  A: 

Save the data in a separate json file, and use PHP's json_decode() function to parse it into PHP objects.

Using this approach, you've also got easy access to the data in JavaScript via AJAX, if you ever decide to move in that direction.

This way is easier to work with than storing data in your PHP file, and it also means that you can trivially update the data programmatically if the need ever arises.

Skilldrick
Thanks for the answer. It looks like this approach would produce an associative array. Is there a means of binding to my own types?
Drew Noakes
You could easily write a method for your own types that would take an array and update the object with those values...
Skilldrick
A: 

var_export and json_encode are possibilities.

file_put_contents($path_to_file, json_encode($albums));
Adam Backstrom
Once coded, the gallery will be static so I'm only interested in deserialising from a file or section of code, not serialising. Still, good to know.
Drew Noakes
+1  A: 

In PHP you would probably define this as an array, ie.

$albums = array(
    0 => array('name' => 'Album 1', 'photos' => array('photo1.jpg', 'photo2.jpg')),
    1 => array('name' => 'Album 2', 'photos' => array('photo3.jpg', 'photo4.jpg'))
);
wimvds
+1  A: 

You can do the exact same in PHP:

$albums = array(
    array(
        'name'   => 'Album 1',
        'photos' => array(
            'photo1.jpg',
            'photo2.jpg',
        )
    ),
    array(
        'name'   => 'Album 2',
        'photos' => array(
            'photo3.jpg',
            'photo4.jpg',
        )
    )
);

But this is bad style. You might want to create a class:

class Album {

    protected $name;

    protected $files;

    public function __construct($name) {
        $this->name = $name;
    }

    public function addFile($file) {
        $this->files[] = $file;
    }

    public function getFiles() {
        return $this->files;
    }

}

$album1 = new Album('Album1');
$album1->addFile('photo1.jpg');
$album1->addFile('photo2.jpg');

$album2 = new Album('Album2');
$album2->addFile('photo3.jpg');
$album2->addFile('photo4.jpg');

$albums = array($album1, $album2);
soulmerge
I saw a config file (from the Gallery project) that used the latter approach for setting config keys. I was hoping to have something a little more concise than that (and strongly typed too if possible). Is there an equivalent to C#'s object initialiser expressions in PHP?
Drew Noakes
@Drew: You can assign basic values (int, string, float, bool, array) as default to members (like this: `protected $files = array()`), but that's it. So you'll need a constructor if you want to assign objects to your members.
soulmerge
@soulmerge - thanks for the clarification.
Drew Noakes
A: 

there are number of possibilities

stereofrog
A: 

Don't forget about serialize

serialize — Generates a storable representation of a value

serialize works on PHP objects too which JSON and XML don't

meouw