- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
__destruct()
函数的“可见性”应该是公开的还是其他?我正在尝试为我的团队编写标准文档,然后出现了这个问题。
最佳答案
除了 Mark Biek 的回答:
__destruct() 函数必须公开。否则,该函数将不会在脚本关闭时执行:
Warning: Call to protected MyChild1::__destruct() from context '' during shutdown ignored in Unknown on line 0
Warning: Call to private MyChild2::__destruct() from context '' during shutdown ignored in Unknown on line 0
这可能没有害处,但很不干净。
但最重要的是:如果析构函数被声明为私有(private)或 protected ,则运行时将在垃圾收集器尝试释放对象时抛出 fatal error :
<?php
class MyParent
{
private function __destruct()
{
echo 'Parent::__destruct';
}
}
class MyChild extends MyParent
{
private function __destruct()
{
echo 'Child::__destruct';
parent::__destruct();
}
}
$myChild = new MyChild();
$myChild = null;
$myChild = new MyChild();
?>
输出
Fatal error: Call to private MyChild::__destruct() from context '' in D:\www\scratchbook\destruct.php on line 20
(感谢 Mark Biek 提供的优秀示例!)
关于php - __destruct PHP 的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/230245/
我已经为我自己的框架编写了一个简单的配置类。 有一些简单的函数,如get()、set() 或loadFile()。但是所有函数和变量都是静态的。 现在我想实现一个自动保存机制。我想创建一个实例(在我的
是否存在该方法不会被调用的情况? 我正在考虑在缓存对象被销毁之前将一个重要变量存储到持久缓存中。这个变量在页面中被多次使用,所以我不想每次变量改变时都用它更新缓存... 最佳答案 我们来上课: cla
你认为这是个好主意吗? 假设您有一个应用程序组件,其他组件使用它来检索/更新数据库中的数据。它基本上是一个具有 get()、set()、update() 方法的类。 如果该组件在调用时仅在其属性中更新
我试图在对象实例化时创建一个文件,并在对象销毁时创建另一个文件。 代码如下: class Foo{ public function __construct(){ file_pu
我找到了以下代码。这是一种特定的模式,还是构建这样的代码的原因 - 或者它只是伪造的? class ExportCSV extends Export { // some private and
__destruct() 函数的“可见性”应该是公开的还是其他?我正在尝试为我的团队编写标准文档,然后出现了这个问题。 最佳答案 除了 Mark Biek 的回答: __destruct() 函数必须
我遇到过这样一种情况,如果 __construct 中的条件为真,我需要调用 __destruct! 如何在构造函数中调用析构函数? 有办法吗? 我可以用一个技巧来做到这一点,但我需要在我的类(cla
如何查看当前是否存在异常,即堆栈是否正在展开? 在下面的示例中,您将如何实现 isExceptionInFlight()? ce, &destructor, ZEND_DESTRUCTOR_FUNC_
我是 OOP 编程的新手。所以我有 2 个关于 __destruct 函数的问题。 我可以从 __destruct 调用另一个对象函数吗?或者其他函数是否已经未声明?例如: function __de
谁能解释为什么 mysql_close() 从类析构函数调用时失败? mysql_error() 报告“连接关闭失败。” link_id = @mysql_connect($server, $user
我刚刚注意到如果在 __destruct() 魔术函数中调用 getcwd() 会返回“/”,而在任何其他方法中它会返回预期的路径。 你对此有解释吗? 最佳答案 这是一个 SAPI 行为 “在脚本关闭
我大大简化了我的代码,但我正在做的是这样的: class App{ protected $apps = []; public function __construct($name, $
如果PHP中的一个对象存储在session中,$_SESSION,什么时候调用它的__destruct方法?我无法在手册中找到任何明确的答案。它确实说 __destruct 将在脚本执行结束时自动调用
在 PHP5 中,是否保证为每个对象实例调用 __destruct() 方法?程序中的异常是否可以阻止这种情况的发生? 最佳答案 还值得一提的是,在子类有自己的析构函数的情况下,父析构函数不会自动调用
class MyDestructableClass { function __construct() { print "\nIn constructor\n"; $t
class MyDestructableClass { function __construct() { print "\nIn constructor\n"; $t
长话短说> echo (new ClassName())->propertyName; 将调用 __destruct(),运行其代码并在成功检索其“propertyName”属性之后(将正常回显)。它
代码会解释一切: expectOutputString('It works great!'); } public function testDestructorOnMockedClas
我发现如果我有以下设置,PHP5 不会调用 __destruct() 函数: class test { __destruct() { echo 'hehe'; exit; }}header('Lo
我正在为我的应用程序设计一个错误处理系统。我想以异常为基础,并且我想通过电子邮件收到有关我没有预料到的每个异常的通知。 我想到了一个类: class My_Exception extends Exce
我是一名优秀的程序员,十分优秀!