php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52706 __sleep inhertiance
Submitted: 2010-08-26 12:14 UTC Modified: 2010-08-26 15:55 UTC
From: dev dot php at linke-t dot net Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.3 OS: Win7
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: dev dot php at linke-t dot net
New email:
PHP Version: OS:

 

 [2010-08-26 12:14 UTC] dev dot php at linke-t dot net
Description:
------------
i the case of class "B" extends a class "A" wich define the function __sleep(), instances of class "B" would class A::__sleep. In this case class serialzing would be very buggy - i tried something like:
Try: let return the __sleep of class "A" all propertys wich are currently definied
resoulting Problem: private properety of class "B" are null or NAN
Try: define the function __sleep() in class "B" 
resoulting Problem: private properety of class "A" are null or NAN


Test script:
---------------
class A{
	public $A_A = 10;
	protected $A_B = 11;
	private $A_C = 12;
	public function A(){
		
	}
	private function __sleep(){
		echo "sleep of A()";
		if(get_class($this)	!= "A")
		{
			$d = array_keys( get_class_vars(get_class($this)));
			return ($d);
		}
		return array("A_A","A_B","A_C");
	}
	public function __get($v){
		
		return "PR";
	}
}
class B extends A{
	public 		$B_A = 20;
	protected 	$B_B = 21;
	private 	$B_C = 22;
	public function B(){
		$this->A();
	}
	public function __sleep(){ 
		return array("A_A","A_B","A_C","B_A","B_B","B_C");
	} /* remove this __sleep would let php use A::__sleep()

}
$a = new A();
$b = new B();
echo "<br/>A";
echo "<br/>" .  htmlspecialchars(serialize($a));
echo "<br/>B";
echo "<br/>" .  htmlspecialchars(serialize($b));


Expected result:
----------------
the complettely serialized Object like with out __sleep()!

Actual result:
--------------
private propertys of parent or child are null or NAN with defined __sleep()

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-08-26 15:55 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2010-08-26 15:55 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Yes, class design has to be well thought.
 [2010-08-29 13:48 UTC] dev dot php at linke-t dot net
I think the primary problems i have are not tested.

I wrote a class with a private property.
class foo{
	private $v;
	function foo(){
		$this->v="bar";
	}
}
And a class wich extends it:
class foo2 extends foo{
	function foo2(){
		$this->foo();
	}
	function __sleep(){
		return array("v");
	}
}

with __sleep serialize will Output: O:10:"foo2":1:{s:1:"v";N;}
without __sleep is the output like: O:10:"foo2":1:{s:6:"foov";s:3:"bar";}

so serialize without __sleep allow acces to private properties. I think, if foo2 doesnt have access to a private property of class foo, than serialize didnt had it too. 

But if i want to save an object ... so i need to save it completly ... so it have to save the private propertys as well. In C++ for as an example i will copy the memory ... so i have copied the private properties as all others. 


and if this isnt a bug ... it's an bad feature ... I think we need to call it "The dont use __sleep()" feature ;) 

I hope i wont get another "its not a bug" - correct the behaivor or wrote in the documention how to save an object with private property of super class, thanks.

"Yes, class design has to be well thought." 
Yes, I aggree - and i think the behavior of functions should it be too.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 27 15:01:29 2024 UTC