Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #13013

Re: Object constructors/destructors

Message-ID <92903185.IQx5DTPeva@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2014-02-27 01:00 +0100
Subject Re: Object constructors/destructors
Newsgroups comp.lang.php
References (17 earlier) <530d1140$0$9522$9b4e6d93@newsspool1.arcor-online.net> <lej3sh$gmp$1@dont-email.me> <lej8ji$e3a$1@dont-email.me> <lejife$v6d$2@dont-email.me> <530e4a38$0$9520$9b4e6d93@newsspool1.arcor-online.net>

Show all headers | View raw


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@news.demon.co.uk> (2004)

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Operator precedence Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-23 18:03 +0100
  Re: Operator precedence GS <nomail@nospam.org.invalid> - 2014-02-23 19:42 +0000
    Re: Operator precedence Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-23 21:23 +0100
    Re: Operator precedence Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-23 16:34 -0500
      Re: Operator precedence Richard Damon <Richard@Damon-Family.org> - 2014-02-23 18:36 -0500
        Re: Operator precedence Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-23 21:09 -0500
          Re: Operator precedence Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-02-24 04:49 +0000
            Re: Operator precedence Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-24 12:56 +0100
              Re: Operator precedence Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-24 17:38 +0100
              Re: Operator precedence Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-02-24 21:25 +0000
                Re: Operator precedence Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-25 00:38 +0100
                Re: Operator precedence Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-25 01:07 +0100
                Re: Operator precedence Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-25 01:37 +0100
                Re: Operator precedence Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-25 02:12 +0100
                Re: Operator precedence Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-25 03:29 +0100
                Re: Operator precedence Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-25 19:41 +0100
                Re: Operator precedence Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-25 19:55 +0100
                Re: Operator precedence Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-25 21:36 +0100
                Re: Operator precedence Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-02-25 03:08 +0000
                Re: Operator precedence Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-25 04:17 +0100
                Re: Operator precedence Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-02-25 04:45 +0000
                Re: Operator precedence GS <nomail@nospam.org.invalid> - 2014-02-25 21:45 +0000
            Re: Operator precedence Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-24 07:00 -0500
              Re: Operator precedence Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-24 18:11 +0100
                Re: Operator precedence Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-24 14:27 -0500
                Re: Operator precedence Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-25 01:25 +0100
                Re: Operator precedence Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-24 20:40 -0500
              Re: Operator precedence Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-02-24 21:23 +0000
                Re: Operator precedence Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-24 16:45 -0500
                Re: Operator precedence Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-02-25 04:32 +0000
                Re: Operator precedence Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-24 23:47 -0500
                Re: Operator precedence Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-02-25 16:56 +0000
                Re: Operator precedence Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-25 13:59 -0500
                Re: Operator precedence Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-25 20:19 +0100
                Re: Operator precedence Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-25 20:43 +0100
                Re: Operator precedence Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-25 16:02 -0500
                Re: Operator precedence Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-25 16:01 -0500
                Object constructors/destructors (was: Operator precedence) Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-25 22:19 +0100
                Re: Object constructors/destructors Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-25 16:32 -0500
                Re: Object constructors/destructors Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-25 22:55 +0100
                Re: Object constructors/destructors Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-25 17:01 -0500
                Re: Object constructors/destructors Adam Harvey <usenet@adamharvey.name> - 2014-02-25 23:22 +0000
                Re: Object constructors/destructors Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-25 21:10 -0500
                Re: Object constructors/destructors Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-26 21:10 +0100
                Re: Object constructors/destructors Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-26 16:01 -0500
                Re: Object constructors/destructors Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-26 22:59 +0100
                Re: Object constructors/destructors Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-26 17:19 -0500
                Re: Object constructors/destructors Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-26 23:39 +0100
                Re: Object constructors/destructors Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-26 22:30 -0500
                Re: Object constructors/destructors Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-27 18:28 +0100
                Re: Object constructors/destructors Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-27 16:09 -0500
                Re: Object constructors/destructors Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-28 14:52 +0100
                Re: Object constructors/destructors Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-28 09:08 -0500
                Re: Object constructors/destructors Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-28 16:52 +0100
                Re: Object constructors/destructors Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-28 11:03 -0500
                Re: Object constructors/destructors Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-27 01:00 +0100
                Re: Object constructors/destructors Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-26 22:39 -0500
                Re: Object constructors/destructors Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-27 18:29 +0100
                Re: Object constructors/destructors Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-27 16:14 -0500
                Re: Object constructors/destructors Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-26 00:18 +0100
                Re: Object constructors/destructors Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-25 21:08 -0500
                Re: Object constructors/destructors Richard Damon <Richard@Damon-Family.org> - 2014-02-26 07:50 -0500
                Re: Object constructors/destructors Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-26 08:44 -0500
                Re: Object constructors/destructors Richard Damon <Richard@Damon-Family.org> - 2014-02-26 23:49 -0500
                Re: Object constructors/destructors Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-27 08:25 -0500
                Re: Object constructors/destructors Richard Damon <Richard@Damon-Family.org> - 2014-03-01 12:56 -0500
                Re: Object constructors/destructors Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-01 14:22 -0500
                Re: Object constructors/destructors Richard Damon <Richard@Damon-Family.org> - 2014-03-02 14:28 -0500
                Re: Object constructors/destructors Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-02 15:42 -0500
                Re: Object constructors/destructors Richard Damon <Richard@Damon-Family.org> - 2014-03-02 17:14 -0500
                Re: Object constructors/destructors Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-02 17:48 -0500
                Re: Object constructors/destructors Tim Streater <timstreater@greenbee.net> - 2014-03-02 23:05 +0000
                Re: Operator precedence Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-02-25 21:08 +0000
                Re: Operator precedence Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-25 22:29 +0100
                Re: Operator precedence Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-02-25 23:15 +0000
              Re: Operator precedence Richard Damon <Richard@Damon-Family.org> - 2014-02-24 22:41 -0500
                Re: Operator precedence Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-24 23:59 -0500
                Re: Operator precedence Richard Damon <Richard@Damon-Family.org> - 2014-03-01 13:39 -0500
                Re: Operator precedence Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-01 14:29 -0500
                Re: Operator precedence Richard Damon <Richard@Damon-Family.org> - 2014-03-02 15:22 -0500
                Re: Operator precedence Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-02 15:49 -0500
                Re: Operator precedence Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-25 19:15 +0100
  Re: Operator precedence Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-02-23 20:56 +0000
    Re: Operator precedence Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-24 14:28 +0100

csiph-web