Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!texta.sil.at!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <92903185.IQx5DTPeva@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Thu, 27 Feb 2014 01:00:17 +0100 User-Agent: KNode/4.11.5 Content-Transfer-Encoding: 8Bit X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. <0.914eafe42061d70d7566.20140224044930GMT.87fvn9ru79.fsf@bsb.me.uk> <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> <530d1140$0$9522$9b4e6d93@newsspool1.arcor-online.net> <530e4a38$0$9520$9b4e6d93@newsspool1.arcor-online.net> MIME-Version: 1.0 Lines: 153 NNTP-Posting-Date: 27 Feb 2014 01:00:22 CET NNTP-Posting-Host: 0e5a4c19.newsspool1.arcor-online.net X-Trace: DXC=JfMIYgeN0T_<<0iRN7DLEQic==]BZ:af^4Fo<]lROoRQnkgeX?EC@@PKV1GJ:PY58QDZm8W4\YJN\YUVHKH;GI^W=blgR_]E3GZ1Pdnj\D 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: >>>>> 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 >> >> 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. A constructor does not need to mean initialization; primarily it means *instantiation*: creating an object and, optionally, creating a relation between it and another object (in the general sense) it inherits from. However, both instantiation and initialization also can be done by a factory method. Factory is one of the GoF design patterns, and it does not violate principles of OOP at all (those people *literally* wrote the book on OOP). A factory is a function, usually (but not necessarily) a static method, that returns (a reference to) an object.¹ Speaking of Python, ISTM that it has generalized the Factory pattern so that both the concept of constructor and the keyword “new” become unnecessary (so that “new” is not a keyword or reserved word at all) there: A class can be called like a function – e.g., Class() –, which invokes the __init__() method of the class if defined, for initialization, and passes to it the parameters passed to the class/constructor call; otherwise it just instantiates that class, returning a reference to the instance. (However, initialization of the instance not dependent on parameters of the instantiation can also be done in the class code itself.) $ python3 Python 3.3.5rc1 (default, Feb 23 2014, 19:47:14) [GCC 4.8.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>> new = 2 >>> class Class(object): pass ... >>> Class >>> Class() <__main__.Class object at 0xb718d04c> See also > But anyway, unserializing an object is *not* creating a new object. IBTD. However, as we will see soon, it is largely irrelevant that unserializing *necessarily* creates a new object. Because the relation between an object and the object it inherits from is just data that can be stored, and later retrieved to restore that relation, much like a constructor initially does. The only important thing is that the relevant parts of the code that serialized the object and that unserialize data to restore the object must be identical; otherwise the restored state is not functionally indistinguishable to the original state of the object. (WLOG, that is the case as the serializing and unserializing code are the same and are running in the same or at least a compatible environment.) > 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. That is a really good analogy. (Too bad that Jerry did not understand it.) It points out that, basically, an object is a collection of data, and methods to operate on/with that data. WLOG, data is serializable, and methods are inherited from another object (in the general sense; in class- based OOP: a class). The relation to that other object again is just data.² Data can be stored in different forms, and retrieved. Therefore, an object can be restored to its previous state, or at least to a state that is functionally indistinguishable from its previous state, without calling a constructor. $ php -r ' class C implements Serializable { private $_data = 42; function serialize() { return serialize($this->_data); } function unserialize ($serialized) { echo "\$serialized = "; var_dump($serialized); $this->_data = unserialize($serialized); } }; $c = new C(); echo "\$c = "; var_dump($c); $s = serialize($c); echo "\$s = "; var_dump($s); $c = unserialize($s); echo "\$c = "; var_dump($c); ' class C#1 (1) { private $_data => int(42) } string(17) "C:1:"C":5:{i:42;}" string(5) "i:42;" class C#2 (1) { private $_data => int(42) } PointedEars ___________ ¹ Those who object to considering a mere function OOP should read the ECMAScript Language Specification, which shows that every function can be made a method of an object; especially, globally declared functions are methods of a global object. ² Even if a method is not inherited (which cannot happen in PHP) it would be possible to serialize it as it is just code. However, this is not always possible by means of the language itself instead of the core language implementation. -- Danny Goodman's books are out of date and teach practices that are positively harmful for cross-browser scripting. -- Richard Cornford, cljs, (2004)