Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

__destruct is never called #489

Open
calvinbaart opened this issue Aug 21, 2019 · 8 comments
Open

__destruct is never called #489

calvinbaart opened this issue Aug 21, 2019 · 8 comments
Assignees
Milestone

Comments

@calvinbaart
Copy link
Contributor

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.

@jakubmisek
Copy link
Member

jakubmisek commented Aug 21, 2019

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
Copy link
Member

jakubmisek commented Aug 27, 2019

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 added a commit that referenced this issue Sep 14, 2019
use of __destruct is discouraged - different behavior than in PHP
ref #489
jakubmisek added a commit that referenced this issue Sep 14, 2019
- Finalize() method that calls __destruct() (through synthesized IDisposable.Dispose())
- Dispose() calls __destruct() just once
- ref #489
@jakubmisek
Copy link
Member

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

jakubmisek added a commit that referenced this issue Sep 14, 2019
- fixes Dispose() IL
- ref #489
@jakubmisek jakubmisek self-assigned this Sep 14, 2019
@jakubmisek
Copy link
Member

jakubmisek commented Sep 17, 2019

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.

@avriltank
Copy link
Contributor

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);
//}

@jakubmisek
Copy link
Member

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 jakubmisek reopened this Dec 30, 2021
@jakubmisek jakubmisek added this to the v1.1.0 milestone Dec 30, 2021
@avriltank
Copy link
Contributor

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

@vvtll
Copy link

vvtll commented Jan 12, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants