FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Operator precedence
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Object constructors/destructors [message #185090 is a reply to message #185085] Tue, 25 February 2014 23:22 Go to previous messageGo to previous message
Adam Harvey is currently offline  Adam Harvey
Messages: 25
Registered: September 2010
Karma:
Junior Member
On Tue, 25 Feb 2014 17:01:52 -0500, Jerry Stuckle wrote:
> On 2/25/2014 4:55 PM, Christoph Michael Becker wrote:
>> I firmly believe that it wouldn't make sense to call a (user-defined)
>> constructor when unserializing or cloning an object. A constructor
>> usually serves to initialize an object -- what already had happened in
>> both cases.
>>
>>
> It makes perfect sense. Not everything is necessarily valid in the new
> object. For instance, a logging object may require opening the log
> file. There are many instances where a resource is no longer available
> and needs to be recreated.

Indeed, which is why PHP provides the Serializable interface (and, for BC
reasons, also __sleep and __wakeup) to allow those sorts of
reinitialisation tasks.

I agree with Christoph: since the object is already instantiated, it
doesn't logically make sense to call the constructor once again.

>> Jerry Stuckle wrote:
>>> I know of no other OO language which would allow this.
>>
>> Others may.
>>
>>
> None that I know of. Please name one.

Python's pickle operates the same way:
http://docs.python.org/2/library/pickle.html#object.__getinitargs__

Providing a way to instantiate objects without calling the constructor
does have valid uses (mostly for testing), which is why PHP 5.4 and later
versions also provide a way to do so via reflection (avoiding the
unserialize() hack):
http://php.net/reflectionclass.newinstancewithoutconstructor

Adam
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Correlating curl resources to some other object.
Next Topic: Experienced Web designer required
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 20:53:14 GMT 2024

Total time taken to generate the page: 0.04517 seconds