Page 1 of 1

[SOLVED] serialize PHP5 private var object problem

Posted: Wed Aug 11, 2004 3:45 am
by CoderGoblin
Last night I ran into problems storing information from an object into a POSTGRES database. Wondering if anyone else has had the problem in Postgres/MySQL/Sessions etc.

Dummy code to give you an idea of what I am rambing on about...

Code: Select all

<?php

class A {
  private  $myvar;
  private  $myvar1;
  // methods.
}

class B extends A {
  // No vats in this as they are taken from A
  // methods
} 

$mything=new (B);
// perform actions on $mything
// save serialized version of $mything to database.
?>
Serialized version of $mything ended up truncated so could not be unserialized. Also tried changing the class variables to protected (same result). When the class variables were public it all worked. Trying to add a "serializeThis" method also didn't work.

Surely serialize also requires the protected/private variables when it is being stored ?

Will try to track down more detail this evening unless anyone else has seen this behaviour and can give my some ideas on how to change the class variable privilege whilst still being able to save the serialized object. (I would like the class variables to only be accessed through the methods). Before you ask I tried putting in combinations such as pg_escape_string/addslashes/htmlentities in case there was a quote problem when saving the information.

[solved] serialize PHP5 private var object problem

Posted: Thu Aug 12, 2004 2:46 am
by CoderGoblin
Found the solution, the serialize was placing null chars in the output. The solution is

Code: Select all

pg_escape_bytea(serialize($myobject));
in conjuction with

Code: Select all

unserialize(pg_unescape_bytea($myserialized_object));
Regards