Why is SplFileInfo Serialization not allowed?

I am trying to save an array of SplFileInfo instances in cache using the serialize command, but the command throws this exception:

 Exception' with message 'Serialization of 'SplFileInfo' is not allowed 

Why is this not allowed?

Thanks!

Note: I'm just curious. The problem itself can be solved.

+4
source share
2 answers

Resources based objects cannot be serialized.

I think I do not need to explain why (if you need an explanation, tell me even if this is OT in this question)

Adding

For @Charles, which does not believe that SplFileInfo does not store / does not open a new resource, I made a small test script.

If you run this:

 new SplFileInfo('index.php'); $link=mysql_connect('localhost','root',''); echo $link; 

Conclusion: Resource ID # 2

If you run this:

 //new SplFileInfo('index.php'); $link=mysql_connect('localhost','root',''); echo $link; 

Conclusion: Resource Identifier No. 1.

+9
source

SplFileInfo cannot be serialized because the PHP team marked it as unserializable. Indeed, this is not surprising: the only actual data that the SplFileInfo object will contain is the file name. Each method in the class is actually a wrapper for a standard non-OO function that does the same. These method calls are resolved during the call, and not when the object is created, so serializing the object will not record the state of the file, as it did during serialization.

If you try to create a list of files to remember later, create a list of file names.

If you are trying to create a list of files and their properties at a specific time, then capture these properties and save them instead of the object. * Keep in mind that PHP caches stat stats for you, so there’s no need to add another layer of caching.


Here is a C file containing the basic mechanisms for SplFileInfo and SplFileObject . The SPL method used to open the file descriptor is called spl_filesystem_file_open . If you search for a file, you will see four links to it. One of them is function definition. One of them is in the constructor for SplFileObject . One of them is in the constructor for SplTempFileObject .

When working with entities in the file system, there are other references to the SPL_FS_FILE definition. The switch statement with SPL_FS_FILE as one of the conditions is the fourth and final call to spl_filesystem_file_open . It is inside a function called spl_filesystem_object_create_type , which creates the actual internal structure on which SPL objects based on the file system work. Note that above the SPL_FS_FILE case, SPL_FS_FILE is the SPL_FS_FILE case, which handles the SplFileInfo cases and notices how this code does not contain an open file descriptor.

This is concrete evidence that SplFileInfo does not contain a file descriptor resource.

The file also contains code that prevents SplFileInfo from being serialized, putting it as non-serializable ... no comment. Heck.


Resolving a resource problem even further in an interactive PHP prompt

  [ charles@duo ~ / splfileinfo_test] $ touch abcde
 [ charles@duo ~ / splfileinfo_test] $ php -a
 Interactive shell

 php> $ fh_a = fopen ('./ a', 'r');
 php> echo $ fh_a;  # should be 1
 Resource id # 2
 php> # WHAT.
 php> $ fh_b = fopen ('./ b', 'r');
 php> echo $ fh_b;  # should be ... uh ... 3 now?
 Resource id # 3
 php> $ fi_c = new SplFileInfo ('./ c');
 php> $ fh_d = fopen ('./ d', 'r');
 php> echo $ fh_d;  # should be 4 if SplFileInfo has no internal resource
 Resource id # 4
 php> exit
 [ charles@duo ~ / splfileinfo_test] $ php -v
 PHP 5.3.6 (cli) (built: Mar 19 2011 07:44:03)
 Copyright (c) 1997-2011 The PHP Group
 Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
     with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans
+4
source

All Articles