Save classes to db?

Save classes to db?

am 26.06.2007 14:28:00 von Gustav Wiberg

--78855875-31410-1182860896-3020
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable

Hi there!
=20
Is it possible to save classes (objects) into a database in PHP? What field=
type should be used then (MySQL) ?
=20
Best regards
/Gustav Wiberg

--78855875-31410-1182860896-3020--

RE: Save classes to db?

am 26.06.2007 15:11:54 von Dale Attree

Hi there,

You would use a blob or text field.

You will have to serialize the object before saving it to db.

Then when you retrieve it, you will have to deserialize it and then eval()
the object so as to active all the values.

-----Original Message-----
From: Gustav Wiberg [mailto:gustav@hmn.se]
Sent: 26 June 2007 02:28 PM
To: 'php windows' (php-windows@lists.php.net)
Subject: [PHP-WIN] Save classes to db?

Hi there!

Is it possible to save classes (objects) into a database in PHP? What
fieldtype should be used then (MySQL) ?

Best regards
/Gustav Wiberg




This e-mail may contain confidential information belonging to the sender which is legally privileged. It is the responsibility of the recipient to ensure that any e-mails or attachments are virus free as Jacklin Enterprises accepts no responsibility. Should you not be the intended recipient then any disclosure, copying, distribution or the taking of any action in reliance of the contents of this email is strictly prohibited. If you have received this transmission in error, please notify the sender immediately.

Jacklin Enterprises Limited is registered in England No. 4398837
Registered office: 18 Bentinck Street, London, W1U 2AR. VAT Registration No: 831 0256 68

This email has been scanned by the MessageLabs Email Security System.

--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Save classes to db?

am 26.06.2007 15:16:00 von Gustav Wiberg

Hi there!

Ok, thanx! I'll try this...

Best regards
/Gustav Wiberg
=20

-----Original Message-----
From: Dale Attree [mailto:dale@jacklinenterprises.com]=20
Sent: Tuesday, June 26, 2007 3:12 PM
To: 'Gustav Wiberg'; ''php windows''
Subject: RE: [PHP-WIN] Save classes to db?

Hi there,

You would use a blob or text field.

You will have to serialize the object before saving it to db.

Then when you retrieve it, you will have to deserialize it and then eval()
the object so as to active all the values.

-----Original Message-----
From: Gustav Wiberg [mailto:gustav@hmn.se]=20
Sent: 26 June 2007 02:28 PM
To: 'php windows' (php-windows@lists.php.net)
Subject: [PHP-WIN] Save classes to db?

Hi there!
=20
Is it possible to save classes (objects) into a database in PHP? What
fieldtype should be used then (MySQL) ?
=20
Best regards
/Gustav Wiberg
=20



This e-mail may contain confidential information belonging to the sender wh=
ich is legally privileged. It is the responsibility of the recipient to ens=
ure that any e-mails or attachments are virus free as Jacklin Enterprises =
accepts no responsibility. Should you not be the intended recipient then an=
y disclosure, copying, distribution or the taking of any action in reliance=
of the contents of this email is strictly prohibited. If you have received=
this transmission in error, please notify the sender immediately.

Jacklin Enterprises Limited is registered in England No. 4398837=20
Registered office: 18 Bentinck Street, London, W1U 2AR. VAT Registration No=
: 831 0256 68

This email has been scanned by the MessageLabs Email Security System.

--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Save classes to db?

am 26.06.2007 15:22:00 von Gustav Wiberg

Hi again!

Can't I use unserialize?

/Gustav
=20

-----Original Message-----
From: Dale Attree [mailto:dale@jacklinenterprises.com]=20
Sent: Tuesday, June 26, 2007 3:12 PM
To: 'Gustav Wiberg'; ''php windows''
Subject: RE: [PHP-WIN] Save classes to db?

Hi there,

You would use a blob or text field.

You will have to serialize the object before saving it to db.

Then when you retrieve it, you will have to deserialize it and then eval()
the object so as to active all the values.

-----Original Message-----
From: Gustav Wiberg [mailto:gustav@hmn.se]=20
Sent: 26 June 2007 02:28 PM
To: 'php windows' (php-windows@lists.php.net)
Subject: [PHP-WIN] Save classes to db?

Hi there!
=20
Is it possible to save classes (objects) into a database in PHP? What
fieldtype should be used then (MySQL) ?
=20
Best regards
/Gustav Wiberg
=20



This e-mail may contain confidential information belonging to the sender wh=
ich is legally privileged. It is the responsibility of the recipient to ens=
ure that any e-mails or attachments are virus free as Jacklin Enterprises =
accepts no responsibility. Should you not be the intended recipient then an=
y disclosure, copying, distribution or the taking of any action in reliance=
of the contents of this email is strictly prohibited. If you have received=
this transmission in error, please notify the sender immediately.

Jacklin Enterprises Limited is registered in England No. 4398837=20
Registered office: 18 Bentinck Street, London, W1U 2AR. VAT Registration No=
: 831 0256 68

This email has been scanned by the MessageLabs Email Security System.

--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Save classes to db?

am 26.06.2007 15:29:06 von Stut

Dale Attree wrote:
> Hi there,
>
> You would use a blob or text field.
>
> You will have to serialize the object before saving it to db.
>
> Then when you retrieve it, you will have to deserialize it and then eval()
> the object so as to active all the values.

It's unserialize not deserialize, and there's no need to eval anything,
just make sure you have either included the class definition before
unserializing it or have an __autoload function defined.

Remember that you cannot serialize resources, so you must make sure you
close any open resources in the __sleep method, and you can recreate
them during unserialize using a __wake method.

-Stut

--
http://stut.net/

> -----Original Message-----
> From: Gustav Wiberg [mailto:gustav@hmn.se]
> Sent: 26 June 2007 02:28 PM
> To: 'php windows' (php-windows@lists.php.net)
> Subject: [PHP-WIN] Save classes to db?
>
> Hi there!
>
> Is it possible to save classes (objects) into a database in PHP? What
> fieldtype should be used then (MySQL) ?
>
> Best regards
> /Gustav Wiberg
>
>
>
>
> This e-mail may contain confidential information belonging to the sender which is legally privileged. It is the responsibility of the recipient to ensure that any e-mails or attachments are virus free as Jacklin Enterprises accepts no responsibility. Should you not be the intended recipient then any disclosure, copying, distribution or the taking of any action in reliance of the contents of this email is strictly prohibited. If you have received this transmission in error, please notify the sender immediately.
>
> Jacklin Enterprises Limited is registered in England No. 4398837
> Registered office: 18 Bentinck Street, London, W1U 2AR. VAT Registration No: 831 0256 68
>
> This email has been scanned by the MessageLabs Email Security System.
>

--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Save classes to db?

am 26.06.2007 15:35:00 von Gustav Wiberg

Hi!

Wouldn't it be possible to serialize the object in destructor of the class =
and unserialize from the constructor?

Best regards
/Gustav Wiberg
=20

-----Original Message-----
From: Stut [mailto:stuttle@gmail.com]=20
Sent: Tuesday, June 26, 2007 3:29 PM
To: Dale Attree
Cc: 'Gustav Wiberg'; ''php windows''
Subject: Re: [PHP-WIN] Save classes to db?

Dale Attree wrote:
> Hi there,
>=20
> You would use a blob or text field.
>=20
> You will have to serialize the object before saving it to db.
>=20
> Then when you retrieve it, you will have to deserialize it and then eval(=
)
> the object so as to active all the values.

It's unserialize not deserialize, and there's no need to eval anything,=20
just make sure you have either included the class definition before=20
unserializing it or have an __autoload function defined.

Remember that you cannot serialize resources, so you must make sure you=20
close any open resources in the __sleep method, and you can recreate=20
them during unserialize using a __wake method.

-Stut

--=20
http://stut.net/

> -----Original Message-----
> From: Gustav Wiberg [mailto:gustav@hmn.se]=20
> Sent: 26 June 2007 02:28 PM
> To: 'php windows' (php-windows@lists.php.net)
> Subject: [PHP-WIN] Save classes to db?
>=20
> Hi there!
> =20
> Is it possible to save classes (objects) into a database in PHP? What
> fieldtype should be used then (MySQL) ?
> =20
> Best regards
> /Gustav Wiberg
> =20
>=20
>=20
>=20
> This e-mail may contain confidential information belonging to the sender =
which is legally privileged. It is the responsibility of the recipient to e=
nsure that any e-mails or attachments are virus free as Jacklin Enterprises=
accepts no responsibility. Should you not be the intended recipient then =
any disclosure, copying, distribution or the taking of any action in relian=
ce of the contents of this email is strictly prohibited. If you have receiv=
ed this transmission in error, please notify the sender immediately.
>=20
> Jacklin Enterprises Limited is registered in England No. 4398837=20
> Registered office: 18 Bentinck Street, London, W1U 2AR. VAT Registration =
No: 831 0256 68
>=20
> This email has been scanned by the MessageLabs Email Security System.
>=20

--=20
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Save classes to db?

am 26.06.2007 15:42:16 von Stut

Gustav Wiberg wrote:
> Wouldn't it be possible to serialize the object in destructor of the class and unserialize from the constructor?

As in serialize($this)?? Think about it for a while and you'll see why
that's not really a great idea.

Firstly, where do you store the serialized object?

Secondly, in the constructor you will be creating a new object, not
populating the current object.

The sort of thing I think you're thinking about is the functionality
provided by __sleep and __wakeup. Look them up in the manual for full
details.

Of course none of that changes the fact that you cannot serialize resources.

-Stut

--
http://stut.net/

> -----Original Message-----
> From: Stut [mailto:stuttle@gmail.com]
> Sent: Tuesday, June 26, 2007 3:29 PM
> To: Dale Attree
> Cc: 'Gustav Wiberg'; ''php windows''
> Subject: Re: [PHP-WIN] Save classes to db?
>
> Dale Attree wrote:
>> Hi there,
>>
>> You would use a blob or text field.
>>
>> You will have to serialize the object before saving it to db.
>>
>> Then when you retrieve it, you will have to deserialize it and then eval()
>> the object so as to active all the values.
>
> It's unserialize not deserialize, and there's no need to eval anything,
> just make sure you have either included the class definition before
> unserializing it or have an __autoload function defined.
>
> Remember that you cannot serialize resources, so you must make sure you
> close any open resources in the __sleep method, and you can recreate
> them during unserialize using a __wake method.
>
> -Stut
>

--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Save classes to db?

am 26.06.2007 15:48:00 von Gustav Wiberg

Hi!

Ok, thanx!

Best regards
/Gustav Wiberg
=20

-----Original Message-----
From: Stut [mailto:stuttle@gmail.com]=20
Sent: Tuesday, June 26, 2007 3:42 PM
To: Gustav Wiberg
Cc: 'Dale Attree'; ''php windows''
Subject: Re: [PHP-WIN] Save classes to db?

Gustav Wiberg wrote:
> Wouldn't it be possible to serialize the object in destructor of the clas=
s and unserialize from the constructor?

As in serialize($this)?? Think about it for a while and you'll see why=20
that's not really a great idea.

Firstly, where do you store the serialized object?

Secondly, in the constructor you will be creating a new object, not=20
populating the current object.

The sort of thing I think you're thinking about is the functionality=20
provided by __sleep and __wakeup. Look them up in the manual for full=20
details.

Of course none of that changes the fact that you cannot serialize resources=
..

-Stut

--=20
http://stut.net/

> -----Original Message-----
> From: Stut [mailto:stuttle@gmail.com]=20
> Sent: Tuesday, June 26, 2007 3:29 PM
> To: Dale Attree
> Cc: 'Gustav Wiberg'; ''php windows''
> Subject: Re: [PHP-WIN] Save classes to db?
>=20
> Dale Attree wrote:
>> Hi there,
>>
>> You would use a blob or text field.
>>
>> You will have to serialize the object before saving it to db.
>>
>> Then when you retrieve it, you will have to deserialize it and then eval=
()
>> the object so as to active all the values.
>=20
> It's unserialize not deserialize, and there's no need to eval anything,=
=20
> just make sure you have either included the class definition before=20
> unserializing it or have an __autoload function defined.
>=20
> Remember that you cannot serialize resources, so you must make sure you=
=20
> close any open resources in the __sleep method, and you can recreate=20
> them during unserialize using a __wake method.
>=20
> -Stut
>=20

--=20
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Save classes to db?

am 26.06.2007 16:19:00 von Gustav Wiberg

Hi again!

I really can't find a manual for __sleep on the php.net site!? But I unders=
tand that __sleep is called before serialize and __wakeup is called before =
unserialize?

Do you mean that I create a function __sleep in my class where I serialize =
my object and create a function __wakeup in my class where i unserialize my=
object?



I was looking for a way to achieve this: (This maybe isn't possible?)

1. Save the serialized object into the database.
2. Load the serialized object from database and unserialize and USE it.=20
(The problem is to USE it).=20


Best regards
/Gustav Wiberg


-----Original Message-----
From: Stut [mailto:stuttle@gmail.com]=20
Sent: Tuesday, June 26, 2007 3:42 PM
To: Gustav Wiberg
Cc: 'Dale Attree'; ''php windows''
Subject: Re: [PHP-WIN] Save classes to db?

Gustav Wiberg wrote:
> Wouldn't it be possible to serialize the object in destructor of the clas=
s and unserialize from the constructor?

As in serialize($this)?? Think about it for a while and you'll see why=20
that's not really a great idea.

Firstly, where do you store the serialized object?

Secondly, in the constructor you will be creating a new object, not=20
populating the current object.

The sort of thing I think you're thinking about is the functionality=20
provided by __sleep and __wakeup. Look them up in the manual for full=20
details.

Of course none of that changes the fact that you cannot serialize resources=
..

-Stut

--=20
http://stut.net/

> -----Original Message-----
> From: Stut [mailto:stuttle@gmail.com]=20
> Sent: Tuesday, June 26, 2007 3:29 PM
> To: Dale Attree
> Cc: 'Gustav Wiberg'; ''php windows''
> Subject: Re: [PHP-WIN] Save classes to db?
>=20
> Dale Attree wrote:
>> Hi there,
>>
>> You would use a blob or text field.
>>
>> You will have to serialize the object before saving it to db.
>>
>> Then when you retrieve it, you will have to deserialize it and then eval=
()
>> the object so as to active all the values.
>=20
> It's unserialize not deserialize, and there's no need to eval anything,=
=20
> just make sure you have either included the class definition before=20
> unserializing it or have an __autoload function defined.
>=20
> Remember that you cannot serialize resources, so you must make sure you=
=20
> close any open resources in the __sleep method, and you can recreate=20
> them during unserialize using a __wake method.
>=20
> -Stut
>=20

--=20
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Save classes to db?

am 26.06.2007 16:26:31 von Tularis

http://www.php.net/manual/en/language.oop5.magic.php

Gustav Wiberg wrote:
> Hi again!
>
> I really can't find a manual for __sleep on the php.net site!? But I understand that __sleep is called before serialize and __wakeup is called before unserialize?
>
> Do you mean that I create a function __sleep in my class where I serialize my object and create a function __wakeup in my class where i unserialize my object?
>
>
>
> I was looking for a way to achieve this: (This maybe isn't possible?)
>
> 1. Save the serialized object into the database.
> 2. Load the serialized object from database and unserialize and USE it.
> (The problem is to USE it).
>
>
> Best regards
> /Gustav Wiberg
>
>
> -----Original Message-----
> From: Stut [mailto:stuttle@gmail.com]
> Sent: Tuesday, June 26, 2007 3:42 PM
> To: Gustav Wiberg
> Cc: 'Dale Attree'; ''php windows''
> Subject: Re: [PHP-WIN] Save classes to db?
>
> Gustav Wiberg wrote:
>> Wouldn't it be possible to serialize the object in destructor of the class and unserialize from the constructor?
>
> As in serialize($this)?? Think about it for a while and you'll see why
> that's not really a great idea.
>
> Firstly, where do you store the serialized object?
>
> Secondly, in the constructor you will be creating a new object, not
> populating the current object.
>
> The sort of thing I think you're thinking about is the functionality
> provided by __sleep and __wakeup. Look them up in the manual for full
> details.
>
> Of course none of that changes the fact that you cannot serialize resources.
>
> -Stut
>

--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Save classes to db?

am 26.06.2007 16:28:00 von Gustav Wiberg

Hi!

Thanx!

/Gustav
=20

-----Original Message-----
From: M. Sokolewicz [mailto:tularis@php.net]=20
Sent: Tuesday, June 26, 2007 4:27 PM
To: Gustav Wiberg
Cc: 'Stut'; 'Dale Attree'; ''php windows''
Subject: Re: [PHP-WIN] Save classes to db?

http://www.php.net/manual/en/language.oop5.magic.php

Gustav Wiberg wrote:
> Hi again!
>=20
> I really can't find a manual for __sleep on the php.net site!? But I unde=
rstand that __sleep is called before serialize and __wakeup is called befor=
e unserialize?
>=20
> Do you mean that I create a function __sleep in my class where I serializ=
e my object and create a function __wakeup in my class where i unserialize =
my object?
>=20
>=20
>=20
> I was looking for a way to achieve this: (This maybe isn't possible?)
>=20
> 1. Save the serialized object into the database.
> 2. Load the serialized object from database and unserialize and USE it.=
=20
> (The problem is to USE it).=20
>=20
>=20
> Best regards
> /Gustav Wiberg
>=20
>=20
> -----Original Message-----
> From: Stut [mailto:stuttle@gmail.com]=20
> Sent: Tuesday, June 26, 2007 3:42 PM
> To: Gustav Wiberg
> Cc: 'Dale Attree'; ''php windows''
> Subject: Re: [PHP-WIN] Save classes to db?
>=20
> Gustav Wiberg wrote:
>> Wouldn't it be possible to serialize the object in destructor of the cla=
ss and unserialize from the constructor?
>=20
> As in serialize($this)?? Think about it for a while and you'll see why=20
> that's not really a great idea.
>=20
> Firstly, where do you store the serialized object?
>=20
> Secondly, in the constructor you will be creating a new object, not=20
> populating the current object.
>=20
> The sort of thing I think you're thinking about is the functionality=20
> provided by __sleep and __wakeup. Look them up in the manual for full=20
> details.
>=20
> Of course none of that changes the fact that you cannot serialize resourc=
es.
>=20
> -Stut
>=20

--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Save classes to db?

am 26.06.2007 16:33:07 von Stut

Gustav Wiberg wrote:
> Hi again!
>
> I really can't find a manual for __sleep on the php.net site!? But I understand that __sleep is called before serialize and __wakeup is called before unserialize?

My bad, it's __wakeup not __wake. But still, a quick search of php.net
for __sleep found this...

http://php.net/oop.magic-functions

> Do you mean that I create a function __sleep in my class where I serialize my object and create a function __wakeup in my class where i unserialize my object?

No. Read that manual page because it explains it quite well. You
implement __sleep and __wakeup in your class. Then, when you call
serialize it calls __sleep before serializing that object, and when you
call unserialize it calls __wakeup.

You *do not* call serialize and unserialize inside your class to do
anything to an instance of that class (well, you can but that just
complicates the point).

> I was looking for a way to achieve this: (This maybe isn't possible?)
>
> 1. Save the serialized object into the database.
> 2. Load the serialized object from database and unserialize and USE it.
> (The problem is to USE it).

Simple. Say you have $obj which is an instance of the Widget class.

$obj_serialized = serialize($obj);
mysql_query('insert into ... set obj =
"'.mysql_real_escape_string($obj_serialized).'" where ...');

Then to read it simply do the select to get the serialized data. Let's
say you put it into a variable called $obj_serialized.

$obj = unserialize($obj_serialized);

You can now use $obj in exactly the same way as you ever would.

$obj->Method('apples', 'pears', 'me', 'old', 'china');

I apologise for confusing things by mentioning sleep and wakeup. These
methods allow you to write code that will get executed when an instance
of your class gets serialized or unserialized. For most classes this
will not be needed.

However, say for example you were writing a DB abstraction class. Since
a DB connection is a resource it will not be stored when serialized.
Therefore you can use an __sleep method to gracefully disconnect the
object from the database when serialized, and __wakeup to reconnect it
when unserialized.

I hope that makes a bit more sense.

-Stut

--
http://stut.net/

--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Save classes to db?

am 27.06.2007 08:39:00 von Gustav Wiberg

Hi again!

I apologize for typing _wake (I meant __wakeup)

I search for __sleep on the php.net - site, but I also search for functions=
... When I did that I came to function "sleep" which is a totally diffrent t=
hing...=20

Thanx for making clearer! Now I think I get it!=20

Thanx a lot!

Best regards
/Gustav Wiberg

=20

-----Original Message-----
From: Stut [mailto:stuttle@gmail.com]=20
Sent: Tuesday, June 26, 2007 4:33 PM
To: Gustav Wiberg
Cc: 'Dale Attree'; ''php windows''
Subject: Re: [PHP-WIN] Save classes to db?

Gustav Wiberg wrote:
> Hi again!
>=20
> I really can't find a manual for __sleep on the php.net site!? But I unde=
rstand that __sleep is called before serialize and __wakeup is called befor=
e unserialize?

My bad, it's __wakeup not __wake. But still, a quick search of php.net=20
for __sleep found this...

http://php.net/oop.magic-functions

> Do you mean that I create a function __sleep in my class where I serializ=
e my object and create a function __wakeup in my class where i unserialize =
my object?

No. Read that manual page because it explains it quite well. You=20
implement __sleep and __wakeup in your class. Then, when you call=20
serialize it calls __sleep before serializing that object, and when you=20
call unserialize it calls __wakeup.

You *do not* call serialize and unserialize inside your class to do=20
anything to an instance of that class (well, you can but that just=20
complicates the point).

> I was looking for a way to achieve this: (This maybe isn't possible?)
>=20
> 1. Save the serialized object into the database.
> 2. Load the serialized object from database and unserialize and USE it.=
=20
> (The problem is to USE it).=20

Simple. Say you have $obj which is an instance of the Widget class.

$obj_serialized =3D serialize($obj);
mysql_query('insert into ... set obj =
"'.mysql_real_escape_string($obj_serialized).'" where ...');

Then to read it simply do the select to get the serialized data. Let's=20
say you put it into a variable called $obj_serialized.

$obj =3D unserialize($obj_serialized);

You can now use $obj in exactly the same way as you ever would.

$obj->Method('apples', 'pears', 'me', 'old', 'china');

I apologise for confusing things by mentioning sleep and wakeup. These=20
methods allow you to write code that will get executed when an instance=20
of your class gets serialized or unserialized. For most classes this=20
will not be needed.

However, say for example you were writing a DB abstraction class. Since=20
a DB connection is a resource it will not be stored when serialized.=20
Therefore you can use an __sleep method to gracefully disconnect the=20
object from the database when serialized, and __wakeup to reconnect it=20
when unserialized.

I hope that makes a bit more sense.

-Stut

--=20
http://stut.net/

--=20
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php