2017-02-03 22 views
0

我有一個文件Orders.php,我想在其中使用類「測試」功能。問題在於類文件位於項目根文件夾中,但我無法使用它。Symfony2使用類

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; 
} 

和類文件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的類文件是在項目的根文件夾。 orders.php文件位於public_html \ project \ src \ AppBundle \ Command \ order.php

如果應用程序位於項目或任何其他目錄的根目錄下,應如何使用類測試?我用「/」編寫名稱空間,所以它應該引用根目錄,不是?

+1

爲什麼你不能把test.php放在AppBundle下? – Jeet

回答

2

如果您想從已定義的PSR-0/PSR-4目標外加載它,您需要自動加載test.php。您可以通過autoload single files更新composer.json文件:

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

更新composer.json後,你需要運行下面的命令:

$ composer dumpautoload 
+0

在composer.json我的自動加載部分: 「自動加載」:{ \t \t 「文件」:[ 「SRC/test.php的」], 「PSR-4」:{ 「」: 「SRC /」 }, 「類映射」: 「應用程序/ AppKernel.php」, 「應用程序/ AppCache.php」 ] }, 但類測試仍然沒有dumpautoload 和文件後發現自己是在src/test.php中 – The50

0

發現我沒有使用的名稱空間中我的課堂文件。自動加載器也有幫助。謝謝。