Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

Object Serialize

Hi,
I want to serialize my object recursively.
How can i complete this job?
ps: i use serialize but it does't serialize Area object in $areaList array of course.
<?php
class Design{
	public $areaList=array();
	function __construct(){}
	function __destruct(){}
	
	function newArea(){
		$area=new Area(null);
		$this->areaList[sizeof($this->areaList)]=$area;
		return $area;
	}
}
 
class Area{
	public $className="";
	protected $areaId="";
	protected $parentArea=null;
	protected $subAreaCount=0;
	protected $areaObject=null; //Form
 
	function getHTML($newRow=false){
		if ($newRow==true)
			$className="newrow";
		else
			$className="newArea col";
		
		$areaId="area_".uniqid();
		$responseStr="<div id='$areaId' class='$className'></div>|";
		$responseStr.="{areaId:'$areaId'}";
		return $responseStr;
	}
	
	function __construct($parentArea){
		$this->parentArea=$parentArea;
	}
	
}
?>
another page
<?php
  $design=new Design();
  $newArea=$design->newArea();
  $_SESSION["Design"]=serialize($design); //this is just serialize Design object. Not area objects in Design object
 
?>

Open in new window

0
erenpasa
Asked:
erenpasa
  • 2
  • 2
1 Solution
 
NoiSCommented:
You MUST use autoload function So when PHP unserialize Design Objet it will load Area Objet too.

Put classes in separated files with pattern CLASSNAME.class.php and leave the files on the PHP project path.

function __autoload($class_name){
   // based on class filename
   require $class_name.".class.php";
}
0
 
erenpasaAuthor Commented:
0
 
NoiSCommented:
hmm...
The solution is for another trouble but seems to work.
It think that the autoload is better for a simple reason. All data will be in the Server Side so, why transfer a lot of data between Client/Server?
Whit code above you can put into a general include header and at any time do this
<?php
  $to_save['var_name']=$var_name;
  $to_save['ObjectName']=$ObjectName;
?>

But, Objects that point to another Object or is extended from another class need that the parent or pointed classes available and the solution is __autoload function (a magic function);

Its almost a persistance like JAVA do (I said almost).
Some vars cannot be saved this way (or any other way) like resources (any kind).

The code below is a snipplet that create the main mecanism to use this kind of persistance.
The function __autoload above complete the code.

<?php
  function serializeVars(){
    if(isset($GLOBALS['to_save'])){
        $_SESSION['saved_vars']=serialize($to_save);
    }
  }
 
 // When script ends call this function
 register_shutdown_function(serializeVars'');
 
  // a general session start 
  if(session_id()==""){
     session_start();
  }
  if(isset($_SESSION['saved_vars'])){
     extract($_SESSION['saved_vars']);
     // now all saved Vars, including arrays and Objects in the session will be available again
  }  
  $to_save=arary();
?>

Open in new window

0
 
erenpasaAuthor Commented:
i've applied _autoload. it's okay. thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now