PHP致命错误:在/MyApp/chat-server.php中找不到类“ MyApp\Chat” [英] PHP Fatal error: Class 'MyApp\Chat' not found in /MyApp/chat-server.php
问题描述
我正在尝试运行Ratchet应用程序演示,但无法执行文件
I am trying to run the Ratchet application demo but I can't execute the file
这是我的文件结构
/var/www/src/MyApp/
/var/www/src/MyApp/chat.php
/var/www/src/MyApp/chat-server.php
<?php
use Ratchet\Server\IoServer;
use MyApp\Chat;
#require "chat.php";
require 'vendor/autoload.php';
$server = IoServer::factory(
new Chat(),
8080
);
$server->run();
/var/www/src/MyApp/composer.json
{
"autoload": {
"psr-0": {
"MyApp": "src"
}
},
"require": {
"cboden/Ratchet": "0.3.*"
}
}
此位置存在供应商文件夹
Vendor Folder is exist in this location
/var/www/src/MyApp/vendor/
每当我在终端中执行聊天服务器文件时,都会出现以下错误
Whenever I am executing the chat-server file in terminal I got the following error
PHP Fatal error: Class 'MyApp\Chat' not found in /MyApp/chat-server.php
请帮助我
注意:完整的代码详细信息在此页面中
Note: The complete code details are exist in this page
http://socketo.me/docs/hello-world
有人问了这个问题,但对他来说仍然没有答案也是个问题
在C:\wamp\www\bin\chat-server.php中找不到类'MyChat\Chat'
This question was asked but still no answer for that question too Class 'MyChat\Chat' not found in C:\wamp\www\bin\chat-server.php
推荐答案
自动加载的主要路径是composer.json文件的位置,因此如果该文件位于 / var / www / src / MyApp /
,自动加载将以此为基础。
The main path for the autoloading is the location of the composer.json file, so if that lives in /var/www/src/MyApp/
, the autoloading will use that as a base.
在您的情况下,您说 MyApp
命名空间可以是在 src
目录(即 / var / www / src / MyApp / src
)中找到。这是不正确的,因为文件 /var/www/src/MyApp/src/MyApp/Chat.php
不存在。
In your case, you say the MyApp
namespace can be found in the src
directory (which means /var/www/src/MyApp/src
). That's not true, since the file /var/www/src/MyApp/src/MyApp/Chat.php
does not exists.
您可以通过3种不同方式解决此问题:
You can solve this issue in 3 different ways:
- 移动
composer.json
-您可以将composer.json
文件移动到/ var / www
,以便能够将其用作基础; - 更新自动加载(使用PSR-4)-您也可以使用PSR-4代替PSR-0并将其配置为
autoload:{ psr-4:{ MyApp\\:}}
; -
重组文件-您还可以将文件结构更改为:
- Moving
composer.json
- You can move thecomposer.json
file to/var/www
, to be able to use that as base; - Updating autoloading (using PSR-4) - You can also use PSR-4 instead of PSR-0 and configure it as
"autoload": { "psr-4": { "MyApp\\": "" } }
; Reorganizing your files - You can also change the file structure to:
/var/www/src/MyApp/
src/MyApp/
Chat.php
composer.json
这篇关于PHP致命错误:在/MyApp/chat-server.php中找不到类“ MyApp\Chat”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!