Php 如何在另一个文件中正确使用来自一个文件的类?
我有一个文件DHMOFramework/lib/Core/Core.php,其中包含以下代码:Php 如何在另一个文件中正确使用来自一个文件的类?,php,class,include,Php,Class,Include,我有一个文件DHMOFramework/lib/Core/Core.php,其中包含以下代码: <?php namespace DHMOFramework\lib\Core\Core; /** * Main DHMOFramework class */ $registeredCommands = []; class Core { function registerCommand($command) { global $registeredCommands;
<?php
namespace DHMOFramework\lib\Core\Core;
/**
* Main DHMOFramework class
*/
$registeredCommands = [];
class Core
{
function registerCommand($command) {
global $registeredCommands;
$registeredCommands[$command] = [];
}
function registerSubCommand($commandname, $subcommand, $args, $helpmsg) {
global $registeredCommands;
$structure = array('command' => $commandname, 'subcommand' => $subcommand, 'args' => $args, "helpmsg" => $helpmsg);
array_push($registeredCommands[$commandname], $structure);
echo $registeredCommands;
}
}
<?php
require_once "DHMOFramework/lib/Core/Core.php";
$dhmo = new Core();
$dhmo->registerCommand("command");
$dhmo->registerSubCommand("command", "subcommand", ["arg1" => [true, string], "arg2" => [true, boolean]], "Usage: /command subcommand <arg1> <arg2>");
如何修复此问题?使用完整的类名:
$dhmo = new DHMOFramework\lib\Core\Core\Core();
或者在使用类之前使用use
语句:
use DHMOFramework\lib\Core\Core\Core;
$dhmo = new Core();
在中查看名称空间我刚刚尝试了use语句和完整的类名。我和两个工作都没有得到相同的错误。我在完全限定类名中遗漏了一个核心。已编辑答案。请包括两个文件的绝对路径。ie:/home/ubuntu/workspace/test.php但是相对于您的工作区,DHMOFramework/lib/Core/Core.php在哪里?(如果你只是把整个东西放在/home/ubuntu/workspace/DHMOFramework/lib/Core/Core.php上,那就更好了?)
use DHMOFramework\lib\Core\Core\Core;
$dhmo = new Core();