- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我试图在对象实例化时创建一个文件,并在对象销毁时创建另一个文件。
代码如下:
class Foo{
public function __construct(){
file_put_contents('a_construct.txt', 'c');
}
public function __destruct(){
file_put_contents('a_destruct.txt', 'd');
}
}
通常会创建a_construct.txt
文件。但是在创建 a_destruct.txt
文件时,它的行为很奇怪。
如果我运行以下代码,“a_destruct”文件不会被创建。
$foo = new Foo();
我得到这个错误:
Warning: file_put_contents(a_destruct.txt): failed to open stream: Permission denied
现在,如果我运行以下命令并检查文件夹,文件就在那里。
$foo = new Foo();
unset($foo);
我尝试过的:
construct
交换到 destruct
并返回,但它始终仅适用于 __construct
方法;第二个测试代码显示我确实有创建文件的权限。
但是第二个主题告诉我,当我让对象在脚本末尾被销毁时,我正在“失去”权限(因为我确定正在调用该方法)。
这是什么原因造成的以及如何解决?
最佳答案
要修复它,您可以使用完整路径:
public function __destruct(){
file_put_contents(dirname(__FILE__) . '/a_destruct.txt', 'd');
}
这记录在 manual 中:
The working directory in the script shutdown phase can be different with some SAPIs (e.g. Apache).
这意味着您正在尝试在不同的目录中创建文件 - 并且您没有权限访问该目录。
这就是当您运行 unset($foo)
时它起作用的原因 - 因为它尚未处于关闭阶段。
尽管我不建议在关闭阶段弄乱工作目录,但我想证明这是可能的:
public function __destruct(){
$tmp = getcwd(); // get current working dir
chdir(dirname(__FILE__)); // set it to be same as the file
file_put_contents('a_destruct.txt', 'd');
chdir($tmp); // set the working dir back to what it was before
}
关于php - 脚本在魔术方法 __destruct() 中失去权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38556859/
我已经为我自己的框架编写了一个简单的配置类。 有一些简单的函数,如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
我是一名优秀的程序员,十分优秀!