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 #185124 is a reply to message #185116] Thu, 27 February 2014 17:28 Go to previous messageGo to previous message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma:
Senior Member
Jerry Stuckle wrote:

> On 2/26/2014 5:39 PM, Christoph Michael Becker wrote:
>> Jerry Stuckle wrote:
>>
>>> On 2/26/2014 4:59 PM, Christoph Michael Becker wrote:
>>>> Jerry Stuckle wrote:
>>>>
>>>> > 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 really need to think this way, just take
>>>> Serializable::serialize() resp. __sleep() as a destructor and
>>>> Serializable::unserialize() resp. __wakeup() as a constructor.
>>>> Actually, the documentation of the Serializable interface explaines it
>>>> that way.
>>>>
>>>
>>> Then it is wrong in another respect, because the destructor is called
>>> after __sleep(), when the object goes out of scope.
>>>
>>>> >>> 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.
>>>>
>>>> No comment.
>>>>
>>>
>>> No matter how you look at it, PHP's implementation violates OO
>>> principles and works differently than any other OO language.
>>
>> You have claimed that often enough, but yet you have not brought any
>> proof.
>>
>> E.g. what happens in Java if an object that implements the
>> java.io.Serializable interface is given as argument to
>> java.io.OutputStream.writeObject()? Is the destructor called?
>>
>
> Yes, it is.

I strongly doubt that. On one hand this would mean you can't use the
object as soon as it has been written to the stream, and on the other
hand there are no *destructors* in Java (finalize() is similar to a
destructor, but it is not guaranteed that it will be called).
Furthermore it depends on the garbage collector, when an object will be
*destroyed*.

> But when the new object is created, a constructor is
> called. You cannot unserialize an object without a constructor being
> called first.

I doubt that, too. After all, java.io.ObjectInputStream.readObject()
returns an object, which of course has to be *created* -- but there's no
need to call its *constructor*.

However, I believe our dissent mainly stems from different use of terms.
I am accustomed to understand constructor/destructor as user defined
functions (which can be defined for custom initialization/finalization
tasks), while this may not be the common definition.

When you're talking about OO principles wrt. to
construction/destruction, you're most likely referring to RAII, which is
not necessarily a general OO principle, and might not be possible to be
cleanly implemented in garbage collected languages generally (consider
cyclic references).

Anyway, I somewhat consider this discussion as harping on about
principles, and I still don't see that PHP lacks the really important
concepts regarding object (de)serialization and cloning.

--
Christoph M. Becker
[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: Sun Dec 01 01:29:53 GMT 2024

Total time taken to generate the page: 0.05084 seconds