class MyDestructableClass {
function __construct() {
print "\nIn constructor\n";
$this->name = "MyDestructableClass";
}
function __destruct() {
print "\nDestroying " . $this->name . "\n";
}
}
$obj = new MyDestructableClass();
Run Code Online (Sandbox Code Playgroud)
当上面的脚本处于复杂的环境中时,__destruct不会被调用exit,但是我无法轻易地重现它.有人注意到了吗?
编辑
我将在这里发布所有内容,它是symfony的测试环境,这意味着如果您熟悉框架,您可以轻松地重现它:
require_once dirname(__FILE__).'/../bootstrap/Doctrine.php';
$profiler = new Doctrine_Connection_Profiler();
$conn = Doctrine_Manager::connection();
$conn->setListener($profiler);
$t = new lime_test(0, new lime_output_color());
class MyDestructableClass {
function __construct() {
print "\nIn constructor\n";
$this->name = "MyDestructableClass";
}
function __destruct() {
print "\nDestroying " . $this->name . "\n";
}
}
$obj …Run Code Online (Sandbox Code Playgroud) 对象解构的确切顺序是什么?
从测试开始,我有一个想法:当前范围的FIFO.
class test1
{
public function __destruct()
{
echo "test1\n";
}
}
class test2
{
public function __destruct()
{
echo "test2\n";
}
}
$a = new test1();
$b = new test2();
Run Code Online (Sandbox Code Playgroud)
它会一次又一次地产生相同的结果:
test1
test2
Run Code Online (Sandbox Code Playgroud)
的PHP手册是模糊的(重点煤矿突出不确定度):"为有特定对象的任何其它引用的析构函数方法将被立即调用,或者在关断期间,任何顺序 ".
解构的确切顺序是什么?任何人都可以详细描述PHP使用的销毁顺序的实现吗?而且,如果这个顺序在任何和所有PHP版本之间不一致,那么任何人都可以查明这个订单改变的PHP版本吗?
最近,我在生产者/消费者队列系统上做了快速实现.
<?php
namespace Queue;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Wire\AMQPTable;
class Amqp
{
private $connection;
private $queueName;
private $delayedQueueName;
private $channel;
private $callback;
public function __construct($host, $port, $login, $password, $queueName)
{
$this->connection = new AMQPStreamConnection($host, $port, $login, $password);
$this->queueName = $queueName;
$this->delayedQueueName = null;
$this->channel = $this->connection->channel();
// First, we need to make sure that RabbitMQ will never lose our queue.
// In order to do so, we need to declare it as durable. To do so we pass
// …Run Code Online (Sandbox Code Playgroud) 我知道,对于一些可能听起来很蠢,但是,我在想,如果我哈瓦在一个类中删除(从数据库和文件系统)中的所有对象数据删除()方法,我怎么能破坏/从内部取出异物类.
这是一个PHP问题.像是unset($this);可能和明智的东西?什么是正确的方法呢?
手册说
一旦删除了对特定对象的所有引用,或者在关闭序列中显式销毁对象或以任何顺序,就会调用析构函数方法.
PHP GC不够用吗?有人可以举例说__destruct方法是必要的吗?
是否存在不会调用此方法的情况?
我想在缓存对象被销毁之前将一个重要的变量存储到持久缓存中.这个变量在页面中多次使用,所以每次变量更改时我都不想用它来更新缓存...