Path: csiph.com!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Jerry Stuckle Newsgroups: comp.lang.php Subject: Re: Object constructors/destructors Date: Sun, 02 Mar 2014 17:48:53 -0500 Organization: A noiseless patient Spider Lines: 183 Message-ID: References: <530a29f4$0$9507$9b4e6d93@newsspool1.arcor-online.net> <0.176fb73a1430ceabf46e.20140224212342GMT.87y510qk69.fsf@bsb.me.uk> <0.0e19cf148bd81c84c7e8.20140225043254GMT.87eh2rrevd.fsf@bsb.me.uk> <0.8872fc115a445007d00d.20140225165618GMT.878uszjfm5.fsf@bsb.me.uk> <530cecd1$0$9508$9b4e6d93@newsspool1.arcor-online.net> <530d08cd$0$9523$9b4e6d93@newsspool1.arcor-online.net> <_plPu.249912$as5.113704@en-nntp-14.dc1.easynews.com> <7DLQu.171151$hV.24964@en-nntp-16.dc1.easynews.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 2 Mar 2014 22:48:51 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="3088393e34bc61b3e6a923d0c81ae1e3"; logging-data="12353"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19vErZiFJNFURFSaU5klIill0gVr2gLNqc=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.3.0 In-Reply-To: Cancel-Lock: sha1:wpL27fGeuZcMDXq8lAQG0TZsAFQ= Xref: csiph.com comp.lang.php:13050 On 3/2/2014 5:14 PM, Richard Damon wrote: > On 3/2/14, 3:42 PM, Jerry Stuckle wrote: >> On 3/2/2014 2:28 PM, Richard Damon wrote: >>> On 3/1/14, 2:22 PM, Jerry Stuckle wrote: >>>> On 3/1/2014 12:56 PM, Richard Damon wrote: >>>>> On 2/27/14, 8:25 AM, Jerry Stuckle wrote: >>>>>> On 2/26/2014 11:49 PM, Richard Damon wrote: >>>>>>> On 2/26/14, 8:44 AM, Jerry Stuckle wrote: >>>>>>>> On 2/26/2014 7:50 AM, Richard Damon wrote: >>>>>>>>> >>>>>>>>> An the "name" of the Deserializing constructor in PHP is >>>>>>>>> __wakeup(). >>>>>>>>> >>>>>>>>> You also do NOT get two destructor calls on the same object, the >>>>>>>>> deserializing created a new object. >>>>>>>>> >>>>>>>> >>>>>>>> __wakeup() is not a constructor - and does not do the same thing. >>>>>>>> >>>>>>> >>>>>>> What makes it NOT a constructor? IT seems to be exactly the thing >>>>>>> that >>>>>>> does what you say must be done to make the new object via >>>>>>> unserialization. >>>>>>> >>>>>> >>>>>> If you want to consider __wakeup() to be a constructor, then you must >>>>>> consider __sleep() to be a destructor. In that case, PHP is also >>>>>> wrong >>>>>> because if an object is serialized, it will call both __sleep() and >>>>>> the >>>>>> destructor. This would be two calls to destructors for the same >>>>>> object. >>>>>> >>>>> >>>>> WHY does __sleep() need to be a destructor? >>>>> >>>>> __sleep() is a function to control how an object is serialized, and the >>>>> object, as you seem to know, still exists, so no destructor should >>>>> happen at this point, in fact, if you look at it, __sleep() has not >>>>> expected to have any effect on the current object, its purpose is to >>>>> make sure that __wakeup() will get all the data it needs, and allow the >>>>> removal of data that it won't need. >>>>> >>>> >>>> If you are going to claim __wakeup() is a constructor, then you have to >>>> agree that it's opposite (__sleep()) is a destructor. Otherwise you are >>>> being inconsistent. >>>> >>>> You can't just make up rules as you see fit! >>>> >>> >>> Then why are you claiming that something that isn't a desturctor must be >>> one. >>> >> >> I'm not - you're the one claiming __wakeup() is a constructor, but it's >> opposite, __sleep() is not a destructor. >> >>> Serialization, creates an external representation for an object, that is >>> what __sleep() is part of. This does NOT inherently involve the >>> destruction of the object (at least at that point). >>> >> >> Then __wakeup() does NOT inherently involve creation of an object. >> > > Since __wakeup() is (naturally) invoked as part of the deserializing > operation, and since PHP defines deserialization via a factory function > that returns the deserialized object, and thus creates an object, is, BY > DEFINITION, a construction operation. __wakeup() is part of that > process, to allow the programmer to do any parts of the construction > process that isn't automatically done. > > I suppose if you want to be very pedantic, unserialize() is actually the > unserializing constructor for every object, of which __wakeup() is the > programmer definable class specific piece of it. > >>> Deserialization, on the other hand, creates an object from an external >>> representation. so it will involve the construction of a new object (you >>> can do "in-place" deserialization in some languages, where you first >>> create a dummy object, and then fill it in with the external data, but >>> this is normally a clumsy way to define it). >>> >> >> Not in other languages. It builds from an already existing object where >> the constructor has been called. > > Some cases use something like: > > Type obj; > obj.unserialize(source); > > This default constructs the object first, then serializes into the > object. For this to work, the object must not have any "constant" > members that need to be unserialized. For this method, unserialization > is not construction. > > Other cases you use > > Type obj(source); > > or > > Type *obj = new Type(source); > > or even > > Type *obj = unserializing_factory(source) > > In these cases, the object is directly created from the serialization > source, and thus DOES involve a constructor and you do not run into the > problem of constant members. In the last case, the calling program > doesn't even need to know the exact type of the object, just a base > type, and the factory can determine the exact type from the data source. > unserializing_factory might be a global function, or it might be a > static member of the base type Type. > > This last case is in fact, the method used in PHP. Since PHP is a > totally dynamically typed language, there is a single global function > that does this operation, and the serialization format defines what type > of object is to be created. It uses the "magic method" __wakeup() to > provide it with the type specific parts of the operation. > >> >>> Note that the object oriented paradigm does have asymmetry in it. A >>> given type will tend to have multiple constructors, for the different >>> ways that an object might come into existence. There is usually only one >>> destructor. In PHP, one of these constructors is called __construct() >>> which is the general purpose constructor. (unlike in other statically >>> type languages, we can only define one of these). PHP also lets us >>> define a piece of the deserialing constructor with __wakeup() (PHP does >>> some of the heavy lifting here in parsing the serialized data and >>> setting up the object, __wakeup() is just required to do any final >>> needed operations after the values have been set.) >>> >> >> Constructors are constructors, and the destructor is a destructor. No >> asymmetry there. Except in PHP. >> >> > > The asymmetry is that a type will, often, have many different > constructors defined: Default, copy, conversion, etc each taking some > data source, an making an object out of that. One such constructor might > be a deserializing constructor that creates a new object out of a data > source that was specially built by serializing a different object. > > A type will almost exclusively have a single destructor. This difference > in number is the asymmetry I was referring to. > > PHP does not differ here, all objects are created with some form of > "constructor", although PHP does not call all of them constructors. > > __construct() is used when creating an object via new > __clone() is used when creating an object via clone > __wakeup() is used when creating an object via unserialize() > > All of the magic functions have the role of a constructor. > > Your confusion here is probably due to the less than rigorous method > used to document this in PHP, it isn't clear that the PHP term > "constructor" is different than the same term you learned in your object > oriented class. > > > > > > You are like some others here - you'll argue just to argue. There is nothing new here - and you're still wrong. But I'm not going to try to teach the pig to sing any longer. -- ================== Remove the "x" from my email address Jerry Stuckle jstucklex@attglobal.net ==================