ReflectionException:YamlTrait中不存在类AppBundle \ Core \ getConstants

时间:2015-12-09 20:00:03

标签: php class symfony constants traits

我想在PHP Trait中使用\ReflectionClass来取出AppBundle\Core\CoreInterface的所有常量并将它们放在array中。我正在使用此array来填充我在use YamlTrait;的几个控制器中使用的属性。

所以我想这样:

<?php
namespace AppBundle\Core;

use AppBundle\Core\CoreInterface;

trait YamlTrait
{
    protected $ymlFiles = [];

    public function __construct()
    {
        $constants = (new \ReflectionClass('AppBundle\Core\CoreInterface'))->getConstants();
        $this->ymlFiles = $constants;
    }
}

这很有效,var_dump($this->ymlFiles);输出:

array (size=5)
  'YML_CITATIONS' => string 'citations' (length=9)
  'YML_PARCOURS' => string 'parcours' (length=8)
  'YML_COMPETENCES' => string 'competences' (length=11)
  'YML_REALISATIONS' => string 'realisations' (length=12)
  'YML_CONTACT' => string 'contact' (length=7)

现在,鉴于var_dump(CoreInterface::class);输出:

C:\wamp\www\sf2\src\AppBundle\Core\YamlTrait.php:12:string 'AppBundle\Core\CoreInterface' (length=28)

然后我认为我能做到:

$constants = (new \ReflectionClass(CoreInterface::class))->getConstants();

但是这会输出以下错误:

  

致命错误:未捕获的异常'ReflectionException',消息' Class AppBundle \ Core \ getConstants不存在'

我也试过以下无济于事:

$reflection = new \ReflectionClass(CoreInterface::class);
$this->ymlFiles = $reflection->getConstants();

我的代码出了什么问题?这是因为我不会注意到PHP中Traits的误用吗?

1 个答案:

答案 0 :(得分:0)

所以,我假设您有一个文件CoreInterface.php

<?php

namespace AppBundle\Core;

class CoreInterface
{
    const YML_CITATIONS = 'citations';
    const YML_PARCOURS = 'parcours';
    const YML_COMPETENCES = 'competences';
    const YML_REALISATIONS = 'realisations';
    const YML_CONTACT = 'contact';
}

和特征YamlTrait.php

<?php

namespace AppBundle\Trait;
trait YamlTrait
{
    protected $ymlFiles = [];
    public function __construct()
    {
        $constants = (new \ReflectionClass('AppBundle\Controller\CoreInterface'))->getConstants();
        $this->ymlFiles = $constants;
    }

    public function getYmlFiles()
    {
        return $this->ymlFiles;
    }
}

您可以在控制器中调用您的特征,例如AcmeController.php:

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use AppBundle\Trait\YamlTrait; //Trait you just created

class AcmeController extends Controller
{
    use YamlTrait;

    /**
     * @Route("/", name="homepage")
     * @Method({"GET"})
     * @Template()
     */
    public function indexAction(Request $request)
    {
        /*This wil get you the YmlFiles array*/
        $ymlFiles = $this->getYmlFiles();

        //Do somtething with your array

        return array();
    }
}

此行$ymlFiles = $this->getYmlFiles();将返回您提到的数组:

array (size=5)
  'YML_CITATIONS' => string 'citations' (length=9)
  'YML_PARCOURS' => string 'parcours' (length=8)
  'YML_COMPETENCES' => string 'competences' (length=11)
  'YML_REALISATIONS' => string 'realisations' (length=12)
  'YML_CONTACT' => string 'contact' (length=7)  

尝试拨打CoreInterface::class时收到的错误是因为双冒号&#39; ::&#39; (范围解析运算符)用于访问静态方法或常量,例如CoreInterface::YML_CITATIONSCoreInterface::YML_PARCOURS,见下文:

http://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php