直接上代碼
class FileTest extends SplFileInfo{ public function __destruct(){ var_dump($this->getRealPath()); }}$file = new FileTest('index.php'); //boolean falsenew 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 falsenew FileTest('index.php'); //'E:\Apache24\htdocs\index.php'
如以上代碼所示,同樣new一個對象,賦值給變數和沒賦值給變數結果卻不一樣,有人知道原因嗎?
我在PHP5.6下跑出的結果:
D:\PHP5.6\php.exe E:\M5\carvincai\test\file.phpbool(false)bool(false)
能不能再提供一些更詳細的資訊?
更新:
我修改了一下代碼:
getRealPath()); }}$file = new FileTest(__FILE__); //boolean falsenew FileTest(__FILE__); //'E:\Apache24\htdocs\index.php'
結果:
D:\PHP5.6\php.exe E:\M5\carvincai\test\file.phpstring(29) "E:\M5\carvincai\test\file.php"string(29) "E:\M5\carvincai\test\file.php"
題主你是不是可以再檢查一下建構函式裡傳入的檔案名稱?
php 5.6 環境沒問題,結果一致
首先要確保 'index.php' 檔案是存在的
你肯定是在同一個環境下面執行了,使用了 'index.php' 作為參數傳入的?