On 16.12.19 10:27, Nikita Popov wrote:
On Mon, Dec 16, 2019 at 10:19 AM Dennis Birkholz <p...@dennis.birkholz.biz>
wrote:
On 12.12.19 18:13, Nikita Popov wrote:
On Tue, Dec 10, 2019 at 12:03 PM Dennis Birkholz <
p...@dennis.birkholz.biz>
wrote:
But my just want to repeat my main concern: buildin
data structures that are not serializable are a real problem for users
that use serialization extensively. Maybe the solution to that problem
is a method to check whether a provided object graph can be serialized
(which may not be possible due to throwing an exception in __sleep() or
something like that), some way to ignore unserializable elements or some
way to register callback methods to handle unserializable elements.
I'm must be missing something obvious here: Isn't this a reliable way to
detect whether an object graph is serializable?
try {
$serialized = serialize($value);
} catch (\Throwable $e) {
// not serializable
}
Checking whether the serialization process worked is different from
checking beforehand whether an object graph is serializable as it makes
it a lot easier to display a meaningful error message. Something like
the Serializable interface in Java. But that seems to not be the PHP way
so far, I will have to think about an RFC for a second parameter for
serialize() that accepts a callback that can "serialize"
objects/variables that fail to serialize themselves...
Could you please explain in more detail what the practical distinction
between checking beforehand and catching an exception is? It seems like the
exception should be sufficient to display a meaningful error message --
heck, it already contains an error message you can use.
The practical distinction would be the error is raised in the moment I
build the object graph and assign a value that is not serializable and
not in the moment I try to serialize. If my object graph is very complex
finding the culprit is a lot easier that way.
But typical PHP way is not to force each and every object in the graph
to implement some interface that makes each object serialization safe
(like in the Java Serializable case).
From my side there is no need to continue this discussion. There seems
to be no easy and sensible way to make WeakMap serializable so I rest my
case.
Nevertheless I appreciate that you took your time to discuss this Nikita.
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php