【问题标题】:php destructor not workingphp析构函数不起作用
【发布时间】:2013-05-06 10:05:47
【问题描述】:

我的 php 析构函数代码目前有问题。

首先,我有一个这样的基本 html 文档:

<!doctype html>
<html lang="en">
  <head><title>Home</title></head>
<body>

<?php include('Table.php');
$T = new Table();
?>

</body>
</html>

问题是代码“new Table()”应该在构造函数中回显“&lt;table&gt;”,而在析构函数中回显“&lt;/table&gt;”。我的问题是我得到以下代码,但我不知道如何修复它:

<!doctype html>
<html lang="en">
  <head><title>Home</title></head>
<body>


<table>
</body>
</html></table>

【问题讨论】:

    标签: php oop destructor


    【解决方案1】:

    问题是你的析构函数只有在脚本终止后才会被调用,也就是在你的 /html 标签已经被输出之后。您缺少的只是在脚本完全结束之前强制析构函数运行的一行:unset()

    试试这个:

    <?php include('Table.php');
    $T = new Table();
    unset($T);
    ?>
    

    如需要进一步阅读:Triggering Destructors Manually in PHP 5

    【讨论】:

      猜你喜欢
      • 2015-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-22
      • 2012-09-02
      • 2011-09-04
      • 2011-04-03
      相关资源
      最近更新 更多