当前位置:Gxlcms > PHP教程 > 关于PHP的析构函数问题

关于PHP的析构函数问题

时间:2021-07-01 10:21:17 帮助过:16人阅读

直接上代码

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'

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

回复内容:

直接上代码

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'

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

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

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

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

更新:

我修改了一下代码:

getRealPath());
    }
}

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

结果:

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"

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

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

人气教程排行