|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-01-04 17:36 UTC] f dot hardy at origami-systems dot com
Description:
------------
if __sleep return a private property in its array, php say :
Unknown: "foo" returned as member variable from __sleep() but does not exist
Reproduce code:
---------------
<?php
class foo
{
private $foo = array();
public function __construct()
{
;
}
public function __sleep()
{
return array('foo');
}
}
?>
Expected result:
----------------
php must find a private property wich is return by __sleep.
Actual result:
--------------
Unknown: "foo" returned as member variable from __sleep() but does not exist
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 03:00:01 2025 UTC |
It is the same bug than #26737, which is closed !! The workaround return array("a", // "\0*\0b", // "\0" . __CLASS__ . "\0c"); is ok !Reproduced on Windows 2000 when using __sleept() with aggregated objects as private memembers. The bug has been reproduced in both PHP 5.2-dev (from link below) and PHP 5.1.4. The following script can be used to reproduce the error: <?php class TestParent { private $parentPrivateVar; protected $parentProtectedVar; public $parentPublicVar; public function __construct($private, $protected, $public) { $this->parentPrivateVar = $private; $this->parentProtectedVar = $protected; $this->parentPublicVar = $public; } function __sleep() { return array("parentPrivateVar", "parentProtectedVar", "parentPublicVar"); } } class TestChild extends TestParent { private $childPrivateVar; protected $childProtectedVar; public $childPublicVar; public function __construct($private, $protected, $public) { $this->childPrivateVar = "child_". $private; $this->childProtectedVar = "child_". $protected; $this->childPublicVar = "child_". $public; parent::__construct("parent_". $private, "parent_". $protected, "parent_". $public); } function __sleep() { return array_merge(array("childPrivateVar", "childProtectedVar", "childPublicVar"), parent::__sleep() ); } } class WebSession { private $privateVar; private $privateAggregatedObject; public function __construct($private, TestChild &$o) { $this->privateVar = $private; $this->privateAggregatedObject = $o; } public function __sleep() { return array("privateVar", "privateAggregatedObject"); } public function getPrivateVar() { return $this->privateVar; } public function getObject() { return $this->privateAggregatedObject; } } // Report simple running errors error_reporting(E_ERROR | E_PARSE | E_WARNING | E_NOTICE | E_STRICT | E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE); // Start user session session_start(); if(isset($_SESSION['obj_Info']) === false) { $_SESSION['obj_Info'] = new WebSession("private variable", new TestChild("private", "protected", "public") ); } echo "<pre>"; var_dump($_SESSION['obj_Info']); echo "</pre>"; echo "<hr />"; echo "TEST: ". $_SESSION['obj_Info']->getPrivateVar() ."<br />"; echo "OBJECT: ". $_SESSION['obj_Info']->getObject() ."<br />"; echo "OBJECT PUBLIC VAR: ". $_SESSION['obj_Info']->getObject()->childPublicVar ."<br />"; ?> GIVES THE FOLLOWING OUTPUT IN PHP 5.1.4: object(WebSession)#1 (2) { ["privateVar:private"]=> string(16) "private variable" ["privateAggregatedObject:private"]=> object(TestChild)#2 (6) { ["childPrivateVar:private"]=> string(13) "child_private" ["childProtectedVar:protected"]=> string(15) "child_protected" ["childPublicVar"]=> string(12) "child_public" ["parentPrivateVar:private"]=> string(14) "parent_private" ["parentProtectedVar:protected"]=> string(16) "parent_protected" ["parentPublicVar"]=> string(13) "parent_public" } } TEST: private variable OBJECT: Object id #2 OBJECT PUBLIC VAR: child_public Notice: Unknown: "parentPrivateVar" returned as member variable from __sleep() but does not exist in Unknown on line 0 The notice is only thrown on the initial run with "parentPrivateVar" is actually set. The notice is not thrown if "parentPrivateVar" is null. In PHP 5.2-dev the scrip dies line 76: echo "OBJECT: ". $_SESSION['obj_Info']->getObject() ."<br />"; The notice is still thrown when "parentPrivateVar" is set though.