I want to write a php object in a text file.
How could I write and read the object in the text file?
For example the object is...
$obj = new stdClass();
$obj->name = "My Name";
$obj->birthdate = "YYYY-MM-DD";
$obj->position = "My position";
Now I want to write this $obj in a text file.
I want a simple way to write the object into the file and want to read the file to get the properties as I defined.
Walkere wrote Feb 2008:
file_get_contents is pretty much a shortcut for fopen, fread, and fclose. So no, you don't need to close the file each time file_get_contents already does that.
It sounds like you're asking the wrong question.
fclose is not very effective to free memory (save memory) and also using
fclose would imply using
fopen and as it's written:
file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.
file_get_contents has a clear memory benefit over
fclose. See as well: