问题描述:

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.

Reference: https://forums.digitalpoint.com/threads/stop-file_get_contents.717145/#post-6660359

网友答案:

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.

So file_get_contents has a clear memory benefit over fclose. See as well:

  • http://php.net/file_get_contents
  • http://php.net/fclose
相关阅读:
Top