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 #185115 is a reply to message #185105] Thu, 27 February 2014 00:00 Go to previous messageGo to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma:
Senior Member
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.¹

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

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, <cife6q$253$1$8300dec7(at)news(dot)demon(dot)co(dot)uk> (2004)
[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: Wed Nov 27 20:39:58 GMT 2024

Total time taken to generate the page: 0.04540 seconds