Serialize objects into files in php


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. // classa.inc:
  3.  
  4. class A {
  5. public $one = 1;
  6.  
  7. public function show_one() {
  8. echo $this->one;
  9. }
  10. }
  11.  
  12. // page1.php:
  13.  
  14. include("classa.inc");
  15.  
  16. $a = new A;
  17. $s = serialize($a);
  18. // store $s somewhere where page2.php can find it.
  19. file_put_contents('store', $s);
  20.  
  21. // page2.php:
  22.  
  23. // this is needed for the unserialize to work properly.
  24. include("classa.inc");
  25.  
  26. $s = file_get_contents('store');
  27. $a = unserialize($s);
  28.  
  29. // now use the function show_one() of the $a object.
  30. $a->show_one();
  31. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.