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 #185106 is a reply to message #185105] Wed, 26 February 2014 21:01 Go to previous messageGo to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma:
Senior Member
On 2/26/2014 3:10 PM, Christoph Michael Becker wrote:
> Jerry Stuckle wrote:
>
>> On 2/25/2014 6:22 PM, Adam Harvey wrote:
>>> 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.
>>>
>>
>> But the object is NOT instantiated. It was at one time, then destroyed.
>> When the new script starts, there is no object in existence.
>>
>>>> > 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
>>>
>>
>> Not knowing Python, I can't say. But if it is a true object, then they
>> are also violating OO principles.
>>
>> OO demands creation of a new object requires a constructor call.
>> Period.
>
> Do you understand, what's the purpose of a constructor? It is there to
> *initialize* an instance. If there is nothing to initialize, it is not
> strictly necessary to call any constructor.
>

Yes, I understand the purpose of a constructor - it looks like much more
than you do.

> But anyway, unserializing an object is *not* creating a new object.
> Consider a class man. When a new object is instantiated the constructor
> is called to initialize the person with respective properties (e.g. age
> = 0). Later this man goes to sleep (serialize); after he wakes up
> (unserialize) the constructor is not called again, as this would reset
> the man to his initial properties, and surely after a good night of
> sleep one may feel younger, but the age has not been reset.
>

It is creating a new object. Before unserialize(), the object does not
exist - only a bunch of data. And that data is NOT an object! You
cannot call an object method on the data, for instance. It is no
different than if the data were stored in a file and loaded in Java or
C++, for instance. To create the object requires a call to a constructor.

>> If you think otherwise, I suggest you learn more about how OO
>> is supposed to work. PHP is not a good example.
>
> I refrain from commenting this statement.
>

If you think otherwise, I suggest you learn more about how OO is
supposed to work. PHP is not a good example.

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex(at)attglobal(dot)net
==================
[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: Fri May 10 09:06:43 GMT 2024

Total time taken to generate the page: 0.04284 seconds