如何正确使用另一个文件中的一个文件的类?

时间:2017-01-14 19:24:58

标签: 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;
    $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;
  }

}

我有另一个文件test.php,代码如下:

<?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>");

当我运行test.php文件时,我一直收到此错误:

Fatal error: Class 'Core' not found in /home/ubuntu/workspace/test.php on line 5

Call Stack:
    0.0001     232464   1. {main}() /home/ubuntu/workspace/test.php:0

我如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

使用完整的班级名称:

$dhmo = new DHMOFramework\lib\Core\Core\Core();

或者在使用该类之前使用use语句:

use DHMOFramework\lib\Core\Core\Core;
$dhmo = new Core();

查看Official PHP documentation

中的命名空间