Resources and object resources

From: Date: Tue, 31 Aug 2021 13:56:42 +0000
Subject: Resources and object resources
Groups: php.internals 
Request: Send a blank email to internals+get-115902@lists.php.net to get a copy of this message
Hello, Resources cannot be serialized. This is mentionned on https://www.php.net/manual/en/function.serialize.php , but one can easily miss it. The information does not seem to appear on https://www.php.net/manual/en/language.oop5.serialization.php and https://www.php.net/manual/en/language.types.resource.php And nowhere does it says what happens if you serialize an object with a resource in a property. Now most resources are replaced by classes in the recent versions of PHP. Because of this, it is advised to replace tests like (is_resource($object->res)) with ($object->res !== FALSE). So, I tried to do this in my application with LDAP connections, which will be objects in PHP 8.1. And the application broke, because when unserializing from session an object containting an LDAP connection, the property is set to int(0). So is_resource() and !== FALSE will not have the same result. I cannot find any documentation page mentionning that resources are turned into int(0) upon serialization. It seems to be consistent accross versions: https://3v4l.org/BHtAh But what will happen when LDAP connections are turned into objects in 8.1? Will they also become int(0) upon serialization or will they behave in an other way? I could not find a case of resource that became an object and is allowed on 3v4l to test this. There is no LDAP or CURL in there. Côme

« previous php.internals (#115902) next »