From a5e1974bc0d1d517cf6916b001a560af39097dd7 Mon Sep 17 00:00:00 2001 From: Rutger Kool Date: Thu, 2 Oct 2025 09:20:38 +0200 Subject: [PATCH] Fix PHP serialization for classes implementing serializable interface --- include/base.h | 4 ++-- zend/base.cpp | 4 ++-- zend/classimpl.cpp | 12 ++---------- 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/include/base.h b/include/base.h index 5d2762c4..492f1d98 100644 --- a/include/base.h +++ b/include/base.h @@ -267,7 +267,7 @@ class PHPCPP_EXPORT Base * is handled by the user-space implementation of Serializable::serialize()). * @return Php::Value */ - Php::Value __serialize(); + Php::Value serialize(); /** * Method that is called when an explicit call to $object->unserialize() is made @@ -275,7 +275,7 @@ class PHPCPP_EXPORT Base * is handled by the user-space implementation of Serializable::unserialize()). * @param params The passed parameters */ - void __unserialize(Php::Parameters ¶ms); + void unserialize(Php::Parameters ¶ms); /** * Method that is called when an explicit call to $object->count() is made diff --git a/zend/base.cpp b/zend/base.cpp index 8199f527..fd97de74 100644 --- a/zend/base.cpp +++ b/zend/base.cpp @@ -223,7 +223,7 @@ int Base::__compare(const Base &that) const * is handled by the user-space implementation of Serializable::serialize()). * @return Php::Value */ -Php::Value Base::__serialize() +Php::Value Base::serialize() { // 'this' refers to a Php::Base class, but we expect that is also implements the Serializable // interface (otherwise we would never have registered the __serialize function as a callback) @@ -242,7 +242,7 @@ Php::Value Base::__serialize() * is handled by the user-space implementation of Serializable::unserialize()). * @param params The passed parameters */ -void Base::__unserialize(Php::Parameters ¶ms) +void Base::unserialize(Php::Parameters ¶ms) { // 'this' refers to a Php::Base class, but we expect that is also implements the Serializable // interface (otherwise we would never have registered the __serialize function as a callback) diff --git a/zend/classimpl.cpp b/zend/classimpl.cpp index 22b08063..46cee296 100644 --- a/zend/classimpl.cpp +++ b/zend/classimpl.cpp @@ -1464,8 +1464,8 @@ const struct _zend_function_entry *ClassImpl::entries() { // the method object need to stay in scope for the lifetime of the script (because the register a pointer // to an internal string buffer) -- so we create them as static variables - static Method serialize("serialize", &Base::__serialize, 0, {}); - static Method unserialize("unserialize", &Base::__unserialize, 0, { ByVal("input", Type::Undefined, true) }); + static Method serialize("serialize", &Base::serialize, 0, {}); + static Method unserialize("unserialize", &Base::unserialize, 0, { ByVal("input", Type::Undefined, true) }); // register the serialize and unserialize method in case this was not yet done in PHP user space if (!hasMethod("serialize")) serialize.initialize(&_entries[i++], _name); @@ -1544,14 +1544,6 @@ zend_class_entry *ClassImpl::initialize(ClassBase *base, const std::string &pref #endif } - // for serializable classes, we install callbacks for serializing and unserializing - if (_base->serializable()) - { - // add handlers to serialize and unserialize - entry.serialize = &ClassImpl::serialize; - entry.unserialize = &ClassImpl::unserialize; - } - // do we have a base class? if (_parent) {