We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Uncaught exception 'Exception' with message 'Serialization of 'Phalcon\DI\FactoryDefault' is not allowed'

I created a utility library that extended Phalcon\Mvc\User\Component

I got the error specified in the subject title when saving the session. I read some earlier threads about this where it was suggested to serialize the object first and then save the session. I tried this but got the same error when passing the object to the serialize function.

This problem is a bit of a nuisance because of the loss of the convenience of accessing the DI container. Any ideas on whether this is a bug?

Regards.

edited Jun '14

The class Phalcon\Mvc\User\Component inherits from the class Phalcon\DI\Injectable, which declares the member variables _dependencyInjector and _eventsManager, causing your Exception. You should be able to overwrite the serialize and unserialize behavior:

class UtilLibrary extends \Phalcon\Mvc\User\Component implements \Serializable
{
    public function serialize()
    {
        $this->_dependencyInjector = null;
        $this->_eventsManager = null;

        $data = array();
        $vars = get_object_vars($this);
        foreach($vars as $var) {
            $data[$var] = $this->$var;
        }

        return serialize($data);
    }

    public function unserialize($data)
    {
        $this->_dependencyInjector = \Phalcon\DI::getDefault();
        $this->_eventsManager = $this->_dependencyInjector->getShared('eventsManager');

        $data = unserialize($data);
        $vars = get_object_vars($this);
        foreach($vars as $var) {
            if(isset($data[$var]) === false) {
                trigger_error('Missing property.');
            }
            $this->$var = $data[$var];
        }
    }
}


38.8k

Hi, thanks for your response.

I tried what you suggested and I'm now getting the following error:

( ! ) Fatal error: Cannot use the memory manager when the request is shutting down in /Applications/MAMP/htdocs/escglam/app/library/ShootData.php on line 444
Call Stack
#   Time    Memory  Function    Location
1   0.4973  8521384 ShootData->serialize( ) ../ShootData.php:0
2   68.7342 8644160 __get ( ??? )   ../ShootData.php:444

Take a look at this issue report. The exception is thrown, since Phalcon is not available while shutting the session down. Some people are saying that this problem was solvable by upgrading or downgrading Phalcon or disabling the XDebug extension. In any case try to avoid the storage of complex objects containing Phalcon structures. You could e.g. change the serialize function to only save the "real" data.



38.8k
edited Jun '14

Hi Wenzel - yep, I'd already seen the report you referred to which is why I had originally tried serializing the data before saving it to the session. It would be good to know what kind of data is "troubling" this process as I haven't got any Phalcon structures being serialized. It's a pretty standard class containing string, int and array properties. The only thing I was using Phalcon for in this case was to sanitize setter assignments. I've since replaced them with PHP equivalents and all is well. On the currenyly serializing version of the class, I simply added the new methods as per your suggestion and immediately got the error referenced in my previous message. It's slightly worrying that Phalcon is being so finicky, particularly when there is nothing remotely Phalcon being serialized.

On the other hand, I really appreciate your help and advice.