Page 1 of 1

keeping an object from being lost

Posted: Tue Jul 13, 2004 3:54 am
by talorlik
hey all,

i have the following problem and i was wondering if anyone can please HELP.

i created a class structure just for testing purposes to be later on implemented in a programm im working on. if it works of course!?

im then creating an object based on those classes enter information into it and i then want to keep it. and the problem i have is that because PHP4.2.5 treates object as if they where any other variable as soon as i jump to the template and back its GONE!!!.

so i tried to make a session of it and access it that way - FORGET ABOUT IT!!!

i tried to also send it through the header and thats when PHP STEPS ON ITS OWN FOOT!!!! becasuse you dont have to declare variables of specific types so PHP doesnt know what was just passed and all it said is e.g. user=Object you cant access the info within it.

(i had a similar problem trying to pass a two dimentional array that was created using a JavaScript through the header and on my PHP page it was seen as a comma separated string. needless to say i nearly broke the mavhine.)

OK!!! while i was typing this i came with an idea which worked!!! i included the second page (object_test1.php - see below) in the third page (object_test2.php - see below) and i got the value i wanted. BUT!!!
and this is a big "BUT" hahahahaha no pun intended, that means that ill have to include this in all the pages which use this object!? is there another way? and besides thats not the way that OO should work. i read that in PHP5 they sorted a lot of this problems unfortunately i dont have the privilage of using it because my company's SP does not have it installed on the server yet, BUGGER!!!!!

please does someone have a way.

this is my code:

1st page: object_test.php holds the class declaration

Code: Select all

<?php

class account
  {
   var $account_number;
   
   function account($accno)
     {
      $this->account_number = $accno;      
     }   
  }
  
class status
  {
   var $status_type;
   var $direction;
   
   function status($stat_type, $direct)
     {
      $this->status_type = $stat_type;
      $this->direction = new direction($direct);
     }
  }
  
class direction
  {
   var $direction;
   
   function direction($direct)
     {
      $this->direction = $direct;
     }
  }  
  
class individual 
  {
   var $fname;
   var $sname;
   var $account;
   var $status;   
		
   function individual($fname, $sname, $accno, $stat_type, $direct) 
     {
      $this->fname = $fname;
      $this->sname = $sname;
      $this->account = new account($accno);
      $this->status = new status($stat_type, $direct);
      
      //print("user fname is = ". $this->fname . "\n");      
      //print("user sname is = ". $this->sname . "\n");      
      //print("account number is = ". $this->account->account_number . "\n");      
      //print("user status is = ". $this->status->status_type . "\n");
      //print("direction is = ". $this->status->direction->direction . "<br>\n");
     }
  }

?>
2nd page: object_test1.php creating the objects

Code: Select all

<?php

  require("object_test.php");   
  
  $user = new individual("tal", "orlik", "12", "seek", "wtoh"); 
  
  $user2 = new individual("tim", "bielawsky", "13", "offer", "wtoh");
  
  echo "<table><tr><td><a href ="object_test2.php">Object test</td></tr></table>";  
  
?>
3rd page: object_test2.php just to display

Code: Select all

<?php
  
  include("object_test1.php"); //NOT IN ORIGINAL CODE

  print "<br>".$user->account->account_number;

?>
Thank you in advance,
Tal.


feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Tue Jul 13, 2004 11:17 am
by kettle_drum
To save an object serialize() it and then unserialize() it when you want to use it again. When you serialize it you parse it all down into a long string value that basically stores the instant variables of the object. Then when you want to use it again you import the class and then unserialize the data and since it knows the class structure, reloads the object.

To pass in sessions:

Code: Select all

$myObject = new Class();
session_start();
$_SESSION['myObject'] = serialize($myObject);

Code: Select all

//include class code

session_start();
$myObject = unserialize($_SESSION['myObject']);
(EDIT: WOOOHOOO im a master now!!!)

Posted: Tue Jul 13, 2004 1:07 pm
by Weirdan
talorlik, it's the way the PHP web-applications work ;)
Each instance of each script is completely independent, it serves the request and exits then. You need to pass the information you need to share between them (I believe it's what you called header) or store it somewhere (sessions, files, db, whatever).

thanks

Posted: Thu Jul 15, 2004 4:01 am
by talorlik
hi all,

thanks for the respons.

and just another quick question.

will serialize() and unserialize() work for both the structure of the object and for the info each field holds? im trying to mirror my objects to look like my DB tables i.e. name of field and value.