1

I wonder what is the best format to store PHP objects to database, an object have multiple parameters and methods, XML or binary or other formats?

for example :

$me = new Person();
$me->code= '007';
$me->name='antoine';
$me->store(); //this object is now stored into postgresql in Binary format or XML or ... you tell me !
//after few days : 
$object = $database->retrievePersonByCode('007');
echo "my name is".$object->name;

How to do that ?

3 Answers 3

2

You will need to serialize it first. PHP has a serialize() function which you can use. But you should pay attention to this when using it:

Object's private members have the class name prepended to the member name; protected members have a '*' prepended to the member name. These prepended values have null bytes on either side.

Depending on the size of the object you wish to serialize you can use TEXT, MEDIUMTEXT or even LONGTEXT.

Sign up to request clarification or add additional context in comments.

Comments

0

Personally I've standardized on json since any language can read it back easily. Plus it's human readable (not binary) and not nearly as verbose as XML. Since Postgres actually supports json as a data type, that is an additional bonus.

Comments

-1

This question is the first stone of the existence of ORM and object oriented persistence layers.

Either you store PHP objects as is in a serialized way (text, json, xml, whatever...) and you loose the ability to perform queries on a particular attribute of your objects (ie looking for all Personn objects younger than 18 by example). Changing one atribute in your instance means updating the whole object.

Or you use a code layer that transposes your PHP objects to real tables or sets in the database. There is the ORM familly that uses abstraction layer (Doctrine, Propel and thousands more...) and there is the OMM familly that just uses Postgres (Pomm).

1 Comment

This answer does not explain what are best formats to store php object to postgresql datbase.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.