2012-03-02 46 views
-1

我使用PHP 5.2.3,但使用PHP 5.3的教程老師我正在編寫相同的代碼,但我只能看到錯誤。我的代碼和教程代碼有什麼區別?

路徑:

Main 
---index.php 
---com 
    ---User.php 

<?php 

define('APPLICATION_PATH', realpath('../')); 
$paths = array(
      APPLICATION_PATH, 
      APPLICATION_PATH . '/com', 
      get_include_path() 
); 
print_r($paths); 
set_include_path(implode(PATH_SEPARATOR, $paths)); 

require_once('User.php'); 

$user = new User(); 
$user->getName(); 

錯誤:

Warning: require_once(User.php) [function.require-once]: failed to open stream: No such file or directory in C:\AppServ\www\advanced\index.php on line 11 

Fatal error: require_once() [function.require]: Failed opening required 'User.php' (include_path='C:\AppServ\www;C:\AppServ\www/com;.;C:\php5\pear') in C:\AppServ\www\advanced\index.php on line 11 

爲什麼我的代碼沒有運行?

enter image description here

也許你需要(user.php的):

<?php 
Class User 
{ 
    public function getName() 
    { 
     return "Ricardo Quaresma"; 
    } 
} 
?> 

解決方法:http://ideone.com/n77hE

+0

哪裏是文件user.php的? – christophmccann 2012-03-02 22:12:27

+0

您的第二個'$ paths'條目具有正斜槓。它應該是反斜槓嗎? – SenorAmor 2012-03-02 22:14:47

+0

它不會改變任何東西.. @SenorAmor – Joseph 2012-03-02 22:20:39

回答

1

我認爲你需要statment應該像這樣:

require_once('com/User.php'); 

順便說一句: 你可以在樹形視圖中顯示我的文件嗎?

+0

我解決了我的問題。我知道使用require_once函數。我正在嘗試set_path_include_path()。 – Joseph 2012-03-02 22:35:39

-1

解決(如下改變):

<?php 
define('APPLICATION_PATH', realpath('')); 
$paths = array(
      APPLICATION_PATH, 
      APPLICATION_PATH . '\com', 
      get_include_path() 
); 
print_r($paths); 
set_include_path(implode(PATH_SEPARATOR, $paths)); 

require_once('User.php'); 

$user = new User(); 
echo $user->getName();