[SOLVED] serialize PHP5 private var object problem

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

[SOLVED] serialize PHP5 private var object problem

Post 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.
Last edited by CoderGoblin on Thu Aug 12, 2004 2:47 am, edited 2 times in total.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

[solved] serialize PHP5 private var object problem

Post 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
Post Reply