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 #185117 is a reply to message #185115] Thu, 27 February 2014 03:39 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 7:00 PM, Thomas 'PointedEars' Lahn wrote:
> 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:
>>>> >> 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.¹
>

Incorrect (as usual). The constructor is designed to initialize an
object. In OO programming, an object is always valid, or, if not valid,
refuses any operations which depend on the object being valid.

A factory is only a means of instantiating the object. It does not
replace or preclude the constructor being called.

> 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.

Which means the constructor IS called.

>>>> new = 2
>>>> class Class(object): pass
> ...
>>>> Class
> <class '__main__.Class'>
>>>> Class()
> <__main__.Class object at 0xb718d04c>
>
> See also <http://docs.python.org/3/tutorial/classes.html#a-first-look-at-classes>
>
>> 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.
>

It is not at all irrelevant. Unserializing an object by definition
creates a new object.

> 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.)
>

But it cannot always be identical. For instance, one of the members of
an object may be a file handle; that file handle is no longer valid and
the file being used must be opened again - creating a new file handle.

>> 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.
>

I understood it, but it is a bad example. Calling a constructor does
NOT necessarily reset the object to an initial state - this happens only
in PHP. In REAL OO languages, the object can be initialized to other
data - a good example is the copy constructor in either C++ or Java.

> $ 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)
> }
>
>

Which just shows that PHP does not implement OO operations properly.

P.S. Your sig separator is broken. It should be exactly
hyphen-hyphen-space-newline.

--
==================
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 04:49:35 GMT 2024

Total time taken to generate the page: 0.04473 seconds