[英]How the Class file is including in another file php using the namesapce and Use?
I have a class called as Errors in Errors.php 我有一个称为Errors.php中的错误的类
namespace MyClass;
class Errors
{
private $_CODES = array(
301 => array(
"code"=> 301,
"message"=> "User With ID Not Found",
"type"=> "Error"
)
);
public static function getError($code)
{
return $this->_CODES[$code];
}
In Myclass.php i have 在Myclass.php中,我有
namespace MyClass;
use MyClass\Errors;
require'vendor/autoload.php';
inside the Myclass i have afunction and i am checking the errors 在Myclass内部,我有功能,我正在检查错误
echo Errors::getError(301);
It shows class Myclass not found error 它显示类Myclass找不到错误
Fatal error: Class 'Myclass\Errors' not found in /var/www/Myclass/Myclass.php
what have i missed here? 我在这里错过了什么?
您必须包括您的Errors.php
Seems you are using Composer's autoloader. 似乎您正在使用Composer的自动加载器。 Did you modify your "composer.json" to make it loads for you?
您是否修改了“ composer.json”以使其为您加载?
Maybe something like: 也许像这样:
"autoload": { "classmap": ["path/to/your/source/directory"] }
“自动加载”:{“ classmap”:[“路径/到/您/源/目录”]}
Or PSR-4 style which is described in official documentation 或官方文档中介绍的PSR-4样式
This Issue is solved. 此问题已解决。 Errors.php looks like below.
Errors.php如下所示。
class Errors{
private static $_CODES = array(
301 => array(
"code"=> 301,
"message"=> "User With ID Not Found",
"type"=> "Error"
)
);
public static function getError($code) {
return self::$_CODES[$code]['message'];
}
}
and i have added require_once "Errors.php"; 并且我添加了require_once“ Errors.php”; in MyClass.php file, now i can call the function
echo Errors::getError(301)
在MyClass.php文件中,现在我可以调用函数
echo Errors::getError(301)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.