php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #41992 __sleep documentation is incorrect
Submitted: 2007-07-13 17:46 UTC Modified: 2007-07-13 17:56 UTC
From: jsnell at e-normous dot com Assigned: colder (profile)
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: OSX 10.4.10
Private report: No CVE-ID: None
 [2007-07-13 17:46 UTC] jsnell at e-normous dot com
Description:
------------
Referencing, http://us2.php.net/manual/en/language.oop5.magic.php, 

Two statements conflict with each other:
"serialize() checks if your class has a function with the magic name __sleep. If so, that function is executed prior to any serialization. It can clean up the object and is supposed to return an array with the names of all variables of that object that should be serialized. If the method doesn't return anything then NULL is serialized and E_NOTICE is issued."

and:

"The __sleep method should return the value to serialize (usually $this), otherwise NULL is serialized."



Reproduce code:
---------------
As per the second statement:

class foo {
	public $stuff =1;
function __sleep() {	  return $this;  }
}
error_reporting(E_ALL);
$bar = new foo();
serialize($bar);

causes the notice:
"serialize() [function.serialize]: __sleep should return an array only containing the names of instance-variables to serialize."

Further, the notice does not appear if the class has no attributes:

class foo {
function __sleep() {	  return $this;  }
}
error_reporting(E_ALL);
$bar = new foo();
serialize($bar);


Expected result:
----------------
If returning an object used to be the proper way to do this, a notice which states that returning an object is deprecated.  If not, then the documentation should not state that returning $this is valid.  Here is patch for the latter:

 cvs diff -u en/language/oop5/magic.xml
Index: en/language/oop5/magic.xml
===================================================================
RCS file: /repository/phpdoc/en/language/oop5/magic.xml,v
retrieving revision 1.15
diff -u -u -r1.15 magic.xml
--- en/language/oop5/magic.xml  20 Jun 2007 22:24:13 -0000      1.15
+++ en/language/oop5/magic.xml  13 Jul 2007 17:43:06 -0000
@@ -51,10 +51,6 @@
     saved completely.
    </para>
    <para>
-    The <literal>__sleep</literal> method should return the value to serialize
-    (usually <literal>$this</literal>), otherwise &null; is serialized.
-   </para>
-   <para>
     Conversely, <function>unserialize</function> checks for the
     presence of a function with the magic name 
     <literal>__wakeup</literal>. If present, this function can

Actual result:
--------------
None, since this code based on incorrect documentation should not be run in the first place.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-07-13 17:56 UTC] colder@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

Thanks for the patch
 [2020-02-07 06:10 UTC] phpdocbot@php.net
Automatic comment on behalf of colder
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=c796f6a654d9275071cfea664a515981cb2118e8
Log: Fix #41992 (__sleep doesn't return the value to serialize) Thanks jsnell for the patch.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 16 15:01:32 2024 UTC