在php上获取对象名称作为字符串?

use*_*149 2 php

所以我得到了这个源代码

//get activated bundles
$bundle_list = new AutoRouter('dev',true);
$bundle_list = $bundle_list->getActivatedBundles(); 

for($a =0; $a < sizeof($bundle_list); $a++)
{
    var_dump($bundle_list[$a]);
}
Run Code Online (Sandbox Code Playgroud)

转储返回几个这样的对象

object(Symfony\Bundle\FrameworkBundle\FrameworkBundle)[38]
  protected 'name' => null
  protected 'extension' => null
  protected 'path' => null
  protected 'container' => null

object(Symfony\Bundle\SecurityBundle\SecurityBundle)[41]
  protected 'name' => null
  protected 'extension' => null
  protected 'path' => null
  protected 'container' => null

object(Symfony\Bundle\TwigBundle\TwigBundle)[40]
  protected 'name' => null
  protected 'extension' => null
  protected 'path' => null
  protected 'container' => null
Run Code Online (Sandbox Code Playgroud)

我需要将对象名称提取为这样的字符串:

(string) "Symfony\Bundle\FrameworkBundle\FrameworkBundle"
(string) "Symfony\Bundle\SecurityBundle\SecurityBundle"
(string) "Symfony\Bundle\TwigBundle\TwigBundle"
Run Code Online (Sandbox Code Playgroud)

就像是

for($a =0; $a < sizeof($bundle_list); $a++)
{
    var_dump((string) $bundle_list[$a]);
}
Run Code Online (Sandbox Code Playgroud)

Abd*_*aye 6

您有几种方法可以在 php 中打印类名:

  1. get_class:返回对象类的名称。如果在非对象上调用该函数,您将收到警告

  2. ClassName::class:自 PHP 5.5 起,我们可以访问类的完全限定名称。

希望这对你有帮助。