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 #185137 is a reply to message #185136] Fri, 28 February 2014 16:03 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 10:52 AM, Christoph Michael Becker wrote:
> Jerry Stuckle wrote:
>
>> 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.
>
> Of course, I have created a new Foo object before serializing in both
> cases. You're probably talking about the unserializing code (please
> look up the difference), but in this case your statement is wrong, as I
> have assigned null to $foo (what would not have been necessary, though).
>
> However, maybe you want
>
> $foo = null;
>
> replaced by
>
> $foo = new Foo();
>
> (resp. for the Java code). While the only benefit of this change would
> be to keep the processor busy, it does no real harm (unless in a not
> garbage collected environment, where it would create a memory leak).
>
> So what do you think (or rather guess), the last line of the output
> would be when changing the code repectively? Answer: bar = 42. That
> proofs that the constructor is not called on the unserialized object.
>
> Anyway, both programs behave equivalent, so Java also does not follow
> basic OO principles wrt. to object (un)serialization, according to your
> claim. However, you have stated it would, so obviously there's a
> contradiction.
>
>> Your stoopidity is amazing.
>
> You may consider changing your attitude to not make a fool of you in the
> public, again.
>

Yes, you continue to make a fool of yourself in public - as your last
statement shows. You have absolutely NO idea what you are talking
about, or how OO works.

I correspond off-list with some knowledgeable people who have followed
this conversation. They can't believe I even try to educate you. It's
like teaching a pig to sing.

And I'm through wasting my time. Your comments aren't even worth a
response (which is why I also refused to fall into your trap as to how I
define an "expression").

You don't discuss. You argue for the sake of argument.

--
==================
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: Wed Nov 27 20:21:47 GMT 2024

Total time taken to generate the page: 0.05187 seconds