未找到 PHP 类,但已包含
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8490667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP class not found but it's included
提问by siannone
I'm including a PHP class with
我包括一个 PHP 类
require_once($ENGINE."/classUser.php");
but when the code is executed i receive this error:
但是当代码执行时,我收到这个错误:
Fatal error: Class 'User' not found in C:\xampp\htdocs\WebName\resources\engine\ajax\signup.php on line 12
致命错误:在第 12 行的 C:\xampp\htdocs\WebName\resources\engine\ajax\signup.php 中找不到“用户”类
I still can't figure out what's the problem. I'm 99% sure it's correct.
我仍然无法弄清楚是什么问题。我 99% 确定它是正确的。
The "$ENGINE" is correct, and the class is correct too (Netbeans suggests me class methods and variables).
“$ENGINE”是正确的,类也是正确的(Netbeans 建议我使用类方法和变量)。
signup.php:
注册.php:
<?php
/* Created on: 13/12/2011
* Author:
*
* Description: User signup procedure.
*/
require_once("../settings.php");
require_once($ENGINE."/classUser.php");
$user = new User();
$user->createUser($_POST["username"], $_POST["email"], $_POST["password"]);
?>
classUser.php:
类用户.php:
<?php
/* Created on: 13/12/2011
* Author:
*
* Description: This class manages users.
*/
require_once("settings.php");
require_once($LIBRARY."/cassandraphp/cassandra.php");
class User {
public function createUser($username, $email, $password){
$cassandra = Cassandra::createInstance($CASSANDRASERVER);
$cassandra->set(
"user.".$username,
array(
'ID' => uniqid(),
'Username' => $username,
'Email' => $email,
'Password' => $password
)
);
}
}
?>
采纳答案by Minras
First of all check if $ENGINE."/classUser.php"
is a valid name of existing file.
Try this:
首先检查是否$ENGINE."/classUser.php"
是现有文件的有效名称。尝试这个:
var_dump(file_exists($ENGINE."/classUser.php"));
回答by jarederaj
Check to make sure your environment isn't being picky about your opening tags. My configuration requires:
检查以确保您的环境对您的开始标签不挑剔。我的配置要求:
<?php
If i try to use:
如果我尝试使用:
<?
Then I get the same error as you.
然后我得到和你一样的错误。
回答by Dejan Marjanovic
if ( ! class_exists('User'))
die('There is no hope!');
回答by Raiden616
I had this problem and the solution was namespaces. The included file was included in its own namespace. Obvious thing, easy to overlook.
我遇到了这个问题,解决方案是命名空间。包含的文件包含在其自己的命名空间中。显而易见的事情,容易被忽视。
回答by olga
It may also be, that you by mistake commented out such a line like require_once __DIR__.'/../vendor/autoload.php';
--- your namespaces are not loaded.
也可能是,您错误地注释掉了像 require_once 这样的行__DIR__.'/../vendor/autoload.php';
--- 您的命名空间未加载。
Or you forget to add a classmap to the composer, thus classes are not autoloaded and are not available. For example,
或者您忘记向 Composer 添加类映射,因此类不会自动加载且不可用。例如,
"autoload": {
"psr-4": {
"": "src/"
},
"classmap": [
"dir/YourClass.php",
]
},
"require": {
"php": ">=5.3.9",
"symfony/symfony": "2.8.*",
回答by Russell England
The problem went away when I did
当我这样做时问题就消失了
sudo service apache2 restart
回答by Lorenzo Magon
It 'happened to me! The problem is that somehow you include a file with the same file name of the class thus invalidating the same class!
它'发生在我身上!问题在于,您以某种方式包含了一个与类具有相同文件名的文件,从而使同一个类无效!
Check the path of inclusion and these checks files with the same name!
检查包含路径,这些检查具有相同名称的文件!
回答by Andrew
Check your file permissions
for the correct linux user for classUser.php
检查您file permissions
的 linux 用户是否正确classUser.php
回答by Enrique Mendoza
In fact, it's a very old thread but useful...
事实上,这是一个非常古老的线程,但很有用......
When namespace declaration is part of your php class file "this kind of weird errors tends to appear".
当命名空间声明是您的 php 类文件的一部分时,“这种奇怪的错误往往会出现”。
Solution: Use namespace
with {
, your code shows like this:
解决方案:使用namespace
with {
,您的代码显示如下:
<?php
namespace path_to\lib {
require_once "folder/php_class_file_where_namespace_declaration_is_part_of_it.php";
**YOUR CODE HERE**
<?php } ?>
回答by Andrew
Double check your autoloader's requirements & namespaces.
仔细检查您的自动加载器的要求和命名空间。
- For example, does your autoloader require your namespace to match the folder structure of where the file is located? If so, make sure they match.
- Another example, does your autoloader require your filenames to follow a certain pattern/is it case sensitive? If so, make sure the filename follows the correct pattern.
- And of course if the class is in a namespace make sure to include it
properly with a fully qualified class name (
/Path/ClassName
) or with ause
statement at the top of your file.
- 例如,您的自动加载器是否要求您的命名空间与文件所在位置的文件夹结构相匹配?如果是这样,请确保它们匹配。
- 另一个例子,您的自动加载器是否要求您的文件名遵循某种模式/是否区分大小写?如果是这样,请确保文件名遵循正确的模式。
- 当然,如果类位于命名空间中,请确保使用完全限定的类名 (
/Path/ClassName
) 或use
文件顶部的语句正确包含它。