[英]ReflectionException: Class AppBundle\Core\getConstants does not exist in YamlTrait
我想在PHP Trait中使用\\ReflectionClass
來取出AppBundle\\Core\\CoreInterface
所有常量, AppBundle\\Core\\CoreInterface
它們放在array
。 我正在使用此array
來填充我在use YamlTrait;
多個控制器中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();
但這會輸出以下錯誤:
致命錯誤:消息為“ 類AppBundle \\ Core \\ getConstants不存在 ”的未捕獲異常'ReflectionException'
我也嘗試了以下無濟於事:
$reflection = new \ReflectionClass(CoreInterface::class);
$this->ymlFiles = $reflection->getConstants();
那么我的代碼有什么問題呢? 這是由於我不會意識到的對PHP特性的濫用嗎?
因此,我假設您有一個文件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
時收到的錯誤是因為雙冒號'::'(作用域解析運算符)用於訪問靜態方法或常量,例如CoreInterface::YML_CITATIONS
或CoreInterface::YML_PARCOURS
,請參見下文:
http://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.