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() Method Enforces Return Type

__sleep is a magic method that lists the name of the variables to serialize. It should come as an array, and is enforced as such since PHP 8.0.

PHP code

<?php

class x {
	function __sleep() {
		return 3;
	}
}

serialize(new x);

?>

Before

PHP Notice:  serialize(): __sleep should return an array only containing the names of instance-variables to serialize

Notice: serialize(): __sleep should return an array only containing the names of instance-variables to serialize

After

PHP Warning:  serialize(): x::__sleep() should return an array only containing the names of instance-variables to serialize

Warning: serialize(): x::__sleep() should return an array only containing the names of instance-variables to serialize

PHP version change

This behavior changed in 8.0.

Error Messages

Extension

© 2023-2026, Damien Seguy - Exakat