SoapServer::setPersistence

Sets SoapServer persistence mode

Description

public void SoapServer::setPersistence(int $mode)

This method allows changing the persistence state of a SoapServer object between requests. This method allows saving data between requests utilizing PHP sessions. This method only has an affect on a SoapServer after it has exported functions utilizing SoapServer::setClass.

Note: The persistence of SOAP_PERSISTENCE_SESSION makes only objects of the given class persistent, but not the class static data. In this case, use $this->bar instead of self::$bar.

Note: SOAP_PERSISTENCE_SESSION serializes data on the class object between requests. In order to properly utilize resources (e.g. PDO), __unserialize() and __serialize() magic methods should be utilized.

Parameters

mode

One of the SOAP_PERSISTENCE_* constants.

SOAP_PERSISTENCE_REQUEST - SoapServer data does not persist between requests. This is the default behavior of any SoapServer object after SoapServer::setClass is called. SOAP_PERSISTENCE_SESSION - SoapServer data persists between requests. This is accomplished by serializing the SoapServer class data into $_SESSION['_bogus_session_name'], because of this session_start must be called before this persistence mode is set.

Return Values

No value is returned.

Changelog

Version Description
8.4.0 SOAP_PERSISTENCE_SESSION now works when the session extension is built as a shared module; previously it failed silently. SoapServer::setPersistence now also throws an Error when SOAP_PERSISTENCE_SESSION is requested while the session extension is not available.

Examples

Example #1 SoapServer::setPersistence example

<?php

class MyFirstPersistentSoapServer
{
    private $resource; // (Such as PDO, mysqli, etc..)
    public $myvar1;
    public $myvar2;

    public function __construct()
    {
        $this->resource = CodeToStartOurResourceUp(); // Handle starting our resource
    }

    public function __unserialize(array $data): void
    {
        $this->myvar1 = $data['myvar1'];
        $this->myvar2 = $data['myvar2'];

        $this->resource = CodeToStartOurResourceUp();
    }

    public function __serialize(): array
    {
        // We make sure to leave out $resource here, so our session data remains persistent.
        // Failure to do so will result in the failure during the unserialization of data
        // on the next request; thus, a MyFirstPersistentSoapServer object would not be persistent across requests.
        return [
            'myvar1' => $this->myvar1,
            'myvar2' => $this->myvar2,
        ];
    }
}

try {
    session_start();
    $server = new SoapServer(null, ['uri' => $_SERVER['REQUEST_URI']]);
    $server->setClass('MyFirstPersistentSoapServer');
    // setPersistence MUST be called after setClass, because setClass's
    // behavior sets SOAP_PERSISTENCE_REQUEST upon enacting the method.
    $server->setPersistence(SOAP_PERSISTENCE_SESSION);
    $server->handle();
} catch (SoapFault $e) {
    error_log("SOAP ERROR: " . $e->getMessage());
}

See Also

  • SoapServer::setClass