Symfony2 使用 class

Symfony2 use of class

我有一个文件 Orders.php,我想在其中使用 class "Test" 函数。问题是 class 文件在项目根文件夹中,但我无法使用它。

orders.php:

<?php
namespace AppBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Test;

protected function execute(InputInterface $input, OutputInterface $output)
{
    ini_set('memory_limit', '4000M');
    $test = new \Test(true);
    exit;
}

而class文件test.php是:

<?php

namespace Test;

class Test
{
     public function __construct($connect = null, $query = null)
     {
        if ($connect)
        {
            $this->connect();
        }

        if ($query)
        {
            $this->query($query);
        }
     }
}

现在 test.php class 文件位于项目根文件夹中。 orders.php 文件位于 public_html\project\src\AppBundle\Command\order.php

如果 class 测试位于项目的根目录或任何其他目录中,我应该如何使用它?我用“/”写命名空间所以它应该指的是根目录,不是吗?

如果您想从已定义的 PSR-0/PSR-4 目录外部加载它,则需要自动加载 test.php。您可以 autoload single files 通过更新 composer.json 文件:

{
    "autoload": {
        "files": ["src/test.php"]
    }
}

更新 composer.json 后,您需要 运行 以下命令:

$ composer dumpautoload

发现我不必在 class 文件中使用命名空间。自动加载器也有帮助。谢谢。