开发者

Why 'Serialization of 'SplFileInfo' is not allowed?

开发者 https://www.devze.com 2023-03-10 06:32 出处:网络
I\'m trying to store an array of SplFileI开发者_运维问答nfo instances in a cache with serialize command but the command throws this exception:

I'm trying to store an array of SplFileI开发者_运维问答nfo instances in a cache with serialize command but the command throws this exception:

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

Why is it not allowed?

Thanks!

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


Objects based on Resources can't be serialized.

I guess I don't have to explain why (if you need the explanation tell me even if it's OT in this question)

Addendum

For @Charles that doesn't believe that SplFileInfo doens't store/open a new resource I have made a little test script.

If you run this:

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

The output is: Resource id #2

If you run this:

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

The output is: Resource id #1.


SplFileInfo can not be serialized because the PHP team has marked it as unserializable. Really, this isn't surprising: The only actual data that a SplFileInfo object would contain would be the filename. Each method in the class is effectively a wrapper for the non-OO standard function that does the same thing. These method calls are resolved at call time, not when the object is created, so serializing the object would not capture the state of the file as it was at serialize time.

If you're trying to build a list of files to remember later, build the list from the file names.

If you're trying to build a list of files and their properties at a specific time, then grab those properties and store those instead of the object. * Please keep in mind that PHP caches the results of stat-based functions for you, so there's no overwhelming need to add another caching layer.


Here is the C file containing the underlying mechanisms for SplFileInfo and SplFileObject. The method SPL uses to open a file handle is called spl_filesystem_file_open. If you search the file, you will see four references to it. One is the function definition. One is in the constructor for SplFileObject. One is in the constructor for SplTempFileObject.

There are other references in there, when working with entities on the filesystem, of a definition called SPL_FS_FILE. A switch statement with SPL_FS_FILE as one of the conditions is the fourth and final spl_filesystem_file_open call. It's inside a function called spl_filesystem_object_create_type, which creates the actual internal structure that the various filesystem-based SPL objects work upon. Note that right above the SPL_FS_FILE case is a SPL_FS_INFO case, which handles SplFileInfo cases, and note how that code does not contain a filehandle open.

This is concrete proof that SplFileInfo does not contain a file handle resource.

The file also contains the code that prevents SplFileInfo from being serialized, by marking it as not serializable... without comment. Darn.


Debunking the resource issue even further at the PHP interactive prompt

[charles@duo ~/splfileinfo_test]$ touch a b c d e
[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
0

精彩评论

暂无评论...
验证码 换一张
取 消