php-windows Digest 27 Jun 2007 06:39:56 -0000 Issue 3268

Topics (messages 28143 through 28155):

Save classes to db?
        28143 by: Gustav Wiberg
        28144 by: Dale Attree
        28145 by: Gustav Wiberg
        28146 by: Gustav Wiberg
        28147 by: Stut
        28148 by: Gustav Wiberg
        28149 by: Stut
        28150 by: Gustav Wiberg
        28151 by: Gustav Wiberg
        28152 by: M. Sokolewicz
        28153 by: Gustav Wiberg
        28154 by: Stut
        28155 by: Gustav Wiberg

Administrivia:

To subscribe to the digest, e-mail:
        [EMAIL PROTECTED]

To unsubscribe from the digest, e-mail:
        [EMAIL PROTECTED]

To post to the list, e-mail:
        [EMAIL PROTECTED]


----------------------------------------------------------------------
--- Begin Message ---
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
 

--- End Message ---
--- Begin Message ---
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:[EMAIL PROTECTED] 
Sent: 26 June 2007 02:28 PM
To: 'php windows' ([EMAIL PROTECTED])
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.

--- End Message ---
--- Begin Message ---
Hi there!

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

Best regards
/Gustav Wiberg
 

-----Original Message-----
From: Dale Attree [mailto:[EMAIL PROTECTED] 
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:[EMAIL PROTECTED] 
Sent: 26 June 2007 02:28 PM
To: 'php windows' ([EMAIL PROTECTED])
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.

--- End Message ---
--- Begin Message ---
Hi again!

Can't I use unserialize?

/Gustav
 

-----Original Message-----
From: Dale Attree [mailto:[EMAIL PROTECTED] 
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:[EMAIL PROTECTED] 
Sent: 26 June 2007 02:28 PM
To: 'php windows' ([EMAIL PROTECTED])
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.

--- End Message ---
--- Begin Message ---
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:[EMAIL PROTECTED] Sent: 26 June 2007 02:28 PM
To: 'php windows' ([EMAIL PROTECTED])
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.


--- End Message ---
--- Begin Message ---
Hi!

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

Best regards
/Gustav Wiberg
 

-----Original Message-----
From: Stut [mailto:[EMAIL PROTECTED] 
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

-- 
http://stut.net/

> -----Original Message-----
> From: Gustav Wiberg [mailto:[EMAIL PROTECTED] 
> Sent: 26 June 2007 02:28 PM
> To: 'php windows' ([EMAIL PROTECTED])
> 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

--- End Message ---
--- Begin Message ---
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:[EMAIL PROTECTED] 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


--- End Message ---
--- Begin Message ---
Hi!

Ok, thanx!

Best regards
/Gustav Wiberg
 

-----Original Message-----
From: Stut [mailto:[EMAIL PROTECTED] 
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

-- 
http://stut.net/

> -----Original Message-----
> From: Stut [mailto:[EMAIL PROTECTED] 
> 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

--- End Message ---
--- Begin Message ---
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:[EMAIL PROTECTED] 
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

-- 
http://stut.net/

> -----Original Message-----
> From: Stut [mailto:[EMAIL PROTECTED] 
> 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

--- End Message ---
--- Begin Message ---
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:[EMAIL PROTECTED] 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


--- End Message ---
--- Begin Message ---
Hi!

Thanx!

/Gustav
 

-----Original Message-----
From: M. Sokolewicz [mailto:[EMAIL PROTECTED] 
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!
> 
> 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:[EMAIL PROTECTED] 
> 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
> 

--- End Message ---
--- Begin Message ---
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/

--- End Message ---
--- Begin Message ---
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 thing... 

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

Thanx a lot!

Best regards
/Gustav Wiberg

 

-----Original Message-----
From: Stut [mailto:[EMAIL PROTECTED] 
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!
> 
> 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

--- End Message ---

Reply via email to