Skip to content
Advertisement

Create an array of objects and then fetch object without losing their class

I’m trying to do some experiments with arrays and objects to improve my PHP programming basics.

What I would like to do is to save a collection of objects instantiated by one of my classes in a text file to be able to fetch them later.

My current implementation is to save the objects in an array, encode the array and save it to a JSON file. However, the problem that arises is that when I then go to extract the objects these are no longer objects deriving from my class but are transformed into stdClass objects.

Here is the method I use to save objects to the file:

public function store(string $titolo, string $nomeAutore, string $titoloToDoList): void
    {
        FileChecker::FindOrBuild('Data/Tasks.json', "[]");

        $newTask = new Task($titolo, $nomeAutore, $titoloToDoList);
        $file = file_get_contents('Data/Tasks.json');
        $decodedFile = json_decode($file);
        array_push($decodedFile, $newTask->toArray());
        file_put_contents('Data/Tasks.json', json_encode($decodedFile));

        FileChecker::FindOrBuild('log/log.txt', "Logs: n");
        Logger::logTaskStore($nomeAutore, $titoloToDoList);
    }

and then I extract from the file with a simple json_decode ()

Can anyone suggest some alternative method to save objects in a text file without losing the class?

edit: forgot to put the toArray() code which is simply

public function toArray(): array
    {
        return get_object_vars($this);
    }

Advertisement

Answer

There are as many file formats as there are people who need to store something slightly different in a file. It’s up to you to figure out which one makes sense for your application.

JSON is a file format designed to be very simple and flexible, and portable between lots of different languages. It has no concept of “class” or “custom type”, and its “object” type is just a list of key-value pairs. (Have a look at the file your current code creates, and you’ll see for yourself.)

You can build a file format “on top of” JSON: that is, rather than storing your objects directly, you first build a custom structure with a way of recording the class name, perhaps as a special key on each object called “__class”. Then to decode, you first decode the JSON, then loop through creating objects based on the name you recorded.

You mentioned in comments PHP’s built-in serialize method. That can be a good choice when you want to store full PHP data for internal use within a program, and will happily store your array of objects without extra code.

In both cases, be aware of the security implications if anyone can edit the serialized data and specify names of classes you don’t want them to create. The unserialize function has an option to list the expected class names, to avoid this, but may have other security problems because of its flexibility.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement