peachpie icon indicating copy to clipboard operation
peachpie copied to clipboard

__destruct is never called

Open calvinbaart opened this issue 5 years ago • 8 comments

Sample:

class Test
{
    public function __construct()
    {
        echo "constructor\r\n";
    }

    public function __destruct()
    {
        echo "destructor\r\n";
    }
}

new Test();

PeachPie output:

constructor

PHP output:

constructor
destructor

Laravel uses __destruct in their Job queueing system, so jobs can't be queued now.

calvinbaart avatar Aug 21 '19 07:08 calvinbaart

we did not specify yet how to treat destructors - .NET has its own garbage collection and call to .NET finalizer is non-deterministic. Currently destructors are ignored.

jakubmisek avatar Aug 21 '19 12:08 jakubmisek

proposal for me:

defining __destruct() adds following pattern to the generated .NET class:

class : IDisposable {
  private bool <disposed>;
  ~() {
    ((IDisposable)this).Dispose();
  }
  void IDisposable.Dispose() {
    if (!<disposed>) {
      <disposed> = true; this.__destruct();
    }
  }
}

additionally; having __destruct would raise a compile-time diagnostic warning

jakubmisek avatar Aug 27 '19 16:08 jakubmisek

Current state: __destruct() is called by GC - not-deterministic unlike in PHP

jakubmisek avatar Sep 14 '19 13:09 jakubmisek

To fully support PHP's destructors, we would need a deterministic GC.

It is not planned for the 1.0 version, but it will be planned for a future version.

Closing as won't be implemented, however; _destruct() is recognized by the compiler resulting in generated CLR Finalizer and IDisposable interface on the containing class.

jakubmisek avatar Sep 17 '19 11:09 jakubmisek

Current state: __destruct() is called by GC - not-deterministic unlike in PHP

@jakubmisek Hi,__destruct seems to be never called,Is there any way to simple use it!

<?php

//class Test implements \System\IDisposable
class Test
{
    public function __construct()
    {
        echo 'hello';
    }
    /*
    public function Dispose():void
    {
        $this->__destruct();
        \System\GC::SuppressFinalize($this);
    }
     */
    public function __destruct()
    {
        echo 'bye';
        file_put_contents(__DIR__.'/test.txt','byebye');
    }
}
$o = new Test();
unset($o);//__destruct is never called
//while(true){
    //sleep(3);
//}

avriltank avatar Dec 30 '21 02:12 avriltank

sadly we still have __destruct as unsupported (https://docs.peachpie.io/php/compatibility/) and the runtime does not call it;

although, we should create .NET Finalizer and call __destruct upon garbage collecting. The problem was, it gets called on another thread .. so we have to think about it.

jakubmisek avatar Dec 30 '21 13:12 jakubmisek

Yes,I know why it is so difficult to implement __destuct! multi thread and destructor...

avriltank avatar Dec 30 '21 13:12 avriltank

I think we must call the __destruct function before the GC started.

vvtll avatar Jan 12 '22 00:01 vvtll