peachpie
peachpie copied to clipboard
__destruct is never called
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.
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.
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
Current state: __destruct()
is called by GC - not-deterministic unlike in PHP
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.
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);
//}
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.
Yes,I know why it is so difficult to implement __destuct! multi thread and destructor...
I think we must call the __destruct function before the GC started.