我正在使用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
为什么我的代码没有运行?
也许你需要(User.php):
<?php
Class User
{
public function getName()
{
return "Ricardo Quaresma";
}
}
?>
答案 0 :(得分:1)
我认为您的要求声明应如此:
require_once('com/User.php');
顺便说一下: 你能告诉我树视图中的文件吗?
答案 1 :(得分:-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();