diff --git a/NEWS b/NEWS
index 5607f31081b8..31ebb08f01f3 100644
--- a/NEWS
+++ b/NEWS
@@ -45,6 +45,10 @@ PHP NEWS
. Fixed a leak when a persistent connection failed a liveness check
with no other live PDO handle. (iliaal)
+- SOAP:
+ . Fixed bug GH-23447 (Segfault when a class passed to SoapServer::setClass()
+ fails to initialize). (Lazizbek Ergashev)
+
- Standard:
. Fixed a memory leak in array_merge_recursive() when the recursive merge of
an object converted to an array fails. (David Carlier)
diff --git a/ext/soap/soap.c b/ext/soap/soap.c
index 3d5536ef8628..4703c7779305 100644
--- a/ext/soap/soap.c
+++ b/ext/soap/soap.c
@@ -1478,7 +1478,11 @@ PHP_METHOD(SoapServer, handle)
/* If new session or something weird happned */
if (soap_obj == NULL) {
- object_init_ex(&tmp_soap, service->soap_class.ce);
+ if (UNEXPECTED(object_init_ex(&tmp_soap, service->soap_class.ce) != SUCCESS)) {
+ php_output_discard();
+ _soap_server_exception(service, function, ZEND_THIS);
+ goto fail;
+ }
/* Call constructor */
if (service->soap_class.ce->constructor) {
diff --git a/ext/soap/tests/gh23447.phpt b/ext/soap/tests/gh23447.phpt
new file mode 100644
index 000000000000..4b63b1cde947
--- /dev/null
+++ b/ext/soap/tests/gh23447.phpt
@@ -0,0 +1,26 @@
+--TEST--
+GH-23447 (Segfault when a class passed to SoapServer::setClass() fails to initialize)
+--EXTENSIONS--
+soap
+--FILE--
+ 'http://testuri.org'));
+$server->setClass('foo');
+
+$server->handle(<<<'XML'
+
+
+
+
+XML);
+
+echo "ok\n";
+?>
+--EXPECT--
+
+SOAP-ENV:ServerUndefined constant "undefinedConstant"
+ok