Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

__sleep should return an array only containing the names of instance-variables to serialize.

Description

The return type of __sleep is actually array, even when it is not mentioned. Any other returned value generate this error.

Example

<?php

class X {
    function __sleep() {
        return 1;
    }
}

serialize(new X);

?>

Alternatives

  • Return an array from __sleep.
  • Add the return type array and return an array from __sleep.
  • Remove the __sleep() method.

In more recent PHP versions, this error message is now :ref:%s::__sleep()-should-return-an-array-only-containing-the-names-of-instance-variables-to-serialize.