Home  >  Article  >  Backend Development  >  关于PHP的析构函数问题

关于PHP的析构函数问题

WBOY
WBOYOriginal
2016-06-06 20:16:181197browse

直接上代码

<code class="php">class FileTest extends SplFileInfo{
    public function __destruct(){
        var_dump($this->getRealPath());
    }
}
$file = new FileTest('index.php');  //boolean false
new FileTest('index.php');  //'E:\Apache24\htdocs\index.php'</code>

如以上代码所示,同样new一个对象,赋值给变量和没赋值给变量结果却不一样,有人知道原因吗?

回复内容:

直接上代码

<code class="php">class FileTest extends SplFileInfo{
    public function __destruct(){
        var_dump($this->getRealPath());
    }
}
$file = new FileTest('index.php');  //boolean false
new FileTest('index.php');  //'E:\Apache24\htdocs\index.php'</code>

如以上代码所示,同样new一个对象,赋值给变量和没赋值给变量结果却不一样,有人知道原因吗?

我在PHP5.6下跑出的结果:

<code>D:\PHP5.6\php.exe E:\M5\carvincai\test\file.php
bool(false)
bool(false)</code>

能不能再提供一些更详细的信息?

更新:

我修改了一下代码:

<code class="php"><?php /**
 * Created by PhpStorm.
 * User: carvincai
 * Date: 2016/3/7
 * Time: 15:23
 */
class FileTest extends SplFileInfo {
    public function __destruct() {
        var_dump($this->getRealPath());
    }
}

$file = new FileTest(__FILE__);  //boolean false
new FileTest(__FILE__);  //'E:\Apache24\htdocs\index.php'</code>

结果:

<code>D:\PHP5.6\php.exe E:\M5\carvincai\test\file.php
string(29) "E:\M5\carvincai\test\file.php"
string(29) "E:\M5\carvincai\test\file.php"</code>

题主你是不是可以再检查一下构造函数里传入的文件名?

php 5.6 环境没问题,结果一致
首先要确保 'index.php' 文件是存在的
你肯定是在同一个环境下面执行了,使用了 'index.php' 作为参数传入的?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn