简体   繁体   English

php die()运行两次?

[英]php die() running twice?

How is it possible that the snippet below prints out "readablenot readable" ? 下面的代码段如何打印出“可读不可读”? afaik a die() should stop everything immediately? afaik a die()应该立即停止所有操作吗?

EDIT: posted the full function. 编辑:张贴了全部功能。 This is a function from Zend_Search_Lucene_Storage_File_Filesystem . 这是Zend_Search_Lucene_Storage_File_Filesystem的函数。 We're always getting "file not readable" errors. 我们总是收到“文件不可读”的错误。 The file does seem to be readable but the snippet below prints out "readablenot readable" 该文件似乎确实可读,但是下面的代码段打印出“可读不可读”

EDIT 2: sorry, made some mistakes in the info I posted; 编辑2:对不起,我发布的信息出现了一些错误; all correct now. 现在一切正确。

public function __construct($filename, $mode='r+b')
    {
        global $php_errormsg;

        if(strpos($mode, 'w') === false) {
            die('not readable');
        }
        else die('readable');

        if (strpos($mode, 'w') === false  &&  !is_readable($filename)) {
            // opening for reading non-readable file
            require_once 'Zend/Search/Lucene/Exception.php';
            throw new Zend_Search_Lucene_Exception('File \'' . $filename . '\' is not readable.');
        }

        $trackErrors = ini_get('track_errors');
        ini_set('track_errors', '1');

        $this->_fileHandle = @fopen($filename, $mode);

        if ($this->_fileHandle === false) {
            ini_set('track_errors', $trackErrors);
            require_once 'Zend/Search/Lucene/Exception.php';
            throw new Zend_Search_Lucene_Exception($php_errormsg);
        }

        ini_set('track_errors', $trackErrors);
    }

I'm gonna go out on a limb and say: 我要弯腰说:

It's not possible. 这是不可能的。

Unless : You're echo ing 'ok' someplace else before the die() or it's in a destructor/shutdown function. 除非 :您在die()之前在其他地方echo “确定”,或者它在析构函数/关闭函数中。

From the manual on exit() which is the same as die() : 从与die()相同的exit()手册中:

Terminates execution of the script. 终止脚本的执行。 Shutdown functions and object destructors will always be executed even if exit() is called. 即使调用exit()关闭函数和对象析构函数也将始终执行。

But the code posted, by itself, would never result in an output of 'okok'. 但是单独发布的代码永远不会导致输出“ okok”。

To Troubleshoot : 解决问题

  • Change the echoed line to something more traceable. 将回显的行更改为更可追踪的行。 Include the file name ( __file__ ) and line number ( __line__ ) just to make sure it really is the same line being executed. 包括文件名( __file__ )和行号( __line__ )只是为了确保它确实是同一行正在执行。
  • Add a debugger (something like xdebug ) to give you a stack trace. 添加一个调试器(类似于xdebug )来给您一个堆栈跟踪。 Is the function somehow being called twice (by a destructor or shutdown hook)? 是否以某种方式两次调用了该函数(通过析构函数或关闭钩子)?

die()触发执行关闭挂钩,因此第二个调用可能是属于击倒挂钩的代码路径的一部分。

One answer could be that you have something in your script which caused the offending code to run twice (ie a non-terminated http redirect, or your class is being instantiated twice). 一个答案可能是您的脚本中有某些内容导致引起问题的代码运行了两次(即,非终止的http重定向,或者您的类被实例化了两次)。

Track back from the point (or points) where your class is instantiated and look for possible duplication. 从实例化类的一个或多个点进行追溯,并寻找可能的重复项。 Or, setup a unit test / script that does nothing else but instantiate the class once with the minimal amount of data required for the test. 或者,设置一个单元测试/脚本,该单元不执行任何其他操作,仅使用测试所需的最少数据量一次实例化该类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM