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 #185135 is a reply to message #185134] Fri, 28 February 2014 14:08 Go to previous messageGo to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma:
Senior Member
On 2/28/2014 8:52 AM, Christoph Michael Becker wrote:
> Jerry Stuckle wrote:
>
>> On 2/27/2014 12:28 PM, Christoph Michael Becker wrote:
>>> Jerry Stuckle wrote:
>>>
>>>> On 2/26/2014 5:39 PM, Christoph Michael Becker wrote:
>>>> > Jerry Stuckle wrote:
>>>> >
>>>> >> No matter how you look at it, PHP's implementation violates OO
>>>> >> principles and works differently than any other OO language.
>>>> >
>>>> > You have claimed that often enough, but yet you have not brought any
>>>> > proof.
>>>> >
>>>> > E.g. what happens in Java if an object that implements the
>>>> > java.io.Serializable interface is given as argument to
>>>> > java.io.OutputStream.writeObject()? Is the destructor called?
>>>> >
>>>>
>>>> Yes, it is.
>>>
>>> I strongly doubt that. On one hand this would mean you can't use the
>>> object as soon as it has been written to the stream, and on the other
>>> hand there are no *destructors* in Java (finalize() is similar to a
>>> destructor, but it is not guaranteed that it will be called).
>>> Furthermore it depends on the garbage collector, when an object will be
>>> *destroyed*.
>>>
>>>> But when the new object is created, a constructor is
>>>> called. You cannot unserialize an object without a constructor being
>>>> called first.
>>>
>>> I doubt that, too. After all, java.io.ObjectInputStream.readObject()
>>> returns an object, which of course has to be *created* -- but there's no
>>> need to call its *constructor*.
>>>
>>
>> No, because the constructor is called automatically.
>
> Wrong, see below.
>
>>> However, I believe our dissent mainly stems from different use of terms.
>>> I am accustomed to understand constructor/destructor as user defined
>>> functions (which can be defined for custom initialization/finalization
>>> tasks), while this may not be the common definition.
>>>
>>
>> They may be user defined, or they may be defined by the system (as in
>> the absence of a user-defined constructor).
>>
>>> When you're talking about OO principles wrt. to
>>> construction/destruction, you're most likely referring to RAII, which is
>>> not necessarily a general OO principle, and might not be possible to be
>>> cleanly implemented in garbage collected languages generally (consider
>>> cyclic references).
>>>
>>
>> No, I am not referring to RAII. I am referring to basic OO principles.
>>
>>> Anyway, I somewhat consider this discussion as harping on about
>>> principles, and I still don't see that PHP lacks the really important
>>> concepts regarding object (de)serialization and cloning.
>>>
>>
>> Just that it doesn't follow OO principles - which has caused me problems
>> in the past.
>
> To shorten further discussion I have installed the JDK 7u51, and written
> the following classes:
>
> ---- Foo.java ----
> public class Foo implements java.io.Serializable {
> protected int bar;
> public Foo() {
> System.out.println("Constructor of Foo called");
> bar = 0;
> }
> public int getBar() {
> return bar;
> }
> public void setBar(int value) {
> bar = value;
> }
> }
>
> ---- Demo.java ----
> import java.io.*;
>
> public class Demo {
> public static void main(String[] args)
> throws IOException, ClassNotFoundException {
> Foo foo = new Foo();
> foo.setBar(42);
> FileOutputStream fileOut = new FileOutputStream("./ser");
> ObjectOutputStream out = new ObjectOutputStream(fileOut);
> out.writeObject(foo);
> out.close();
> fileOut.close();
> System.out.println("Serialized foo");
> System.out.print("bar = ");
> System.out.println(foo.getBar());
> foo = null;
> FileInputStream fileIn = new FileInputStream("./ser");
> ObjectInputStream in = new ObjectInputStream(fileIn);
> foo = (Foo) in.readObject();
> in.close();
> fileIn.close();
> System.out.println("Unserialized foo");
> System.out.print("bar = ");
> System.out.println(foo.getBar());
> }
> }
>
> To compare that with PHP's behavior, I've written the following script:
>
> ---- demo.php ----
> <?php
>
> class Foo
> {
> protected $bar;
> public function __construct() {
> echo "Constructor of Foo called\n";
> $this->bar = 0;
> }
> public function getBar() {
> return $this->bar;
> }
> public function setBar($value) {
> $this->bar = $value;
> }
> }
>
> $foo = new Foo();
> $foo->setBar(42);
> file_put_contents('./ser', serialize($foo));
> echo "Serialized foo\nbar = ", $foo->getBar(), "\n";
> $foo = null;
> $foo = unserialize(file_get_contents('./ser'));
> echo "Unserialized foo\nbar = ", $foo->getBar(), "\n";
>
> Running the java programm and the PHP script (PHP 5.4.19 cli) produces
> the following output:
>
> Constructor of Foo called
> Serialized foo
> bar = 42
> Unserialized foo
> bar = 42
>
> Any conclusions are left to the reader.
>

Which shows just how little you understand programming.

In either case you have not created a new Foo object before serializing;
you have only serialized into an existing object. So obviously no
constructor is called.

Your stoopidity is amazing.

--
==================
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: Sun Nov 24 01:13:15 GMT 2024

Total time taken to generate the page: 0.03199 seconds