Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get object name as string on php? [duplicate]

Tags:

php

So I got this source code

//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]);
}

The dump returns several objects like this

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

I need to extract the object names as string like this:

(string) "Symfony\Bundle\FrameworkBundle\FrameworkBundle"
(string) "Symfony\Bundle\SecurityBundle\SecurityBundle"
(string) "Symfony\Bundle\TwigBundle\TwigBundle"

Something like

for($a =0; $a < sizeof($bundle_list); $a++)
{
    var_dump((string) $bundle_list[$a]);
}
like image 365
user3531149 Avatar asked Nov 27 '25 01:11

user3531149


1 Answers

You have several way to print a a class name in php:

  1. get_class: Returns the name of the class of an object. You will have a warning if the function is called on a non object

  2. ClassName::class: Since PHP 5.5, we can access to the fully qualified name of a class.

Hope this helps you.

like image 153
Abdoul Ndiaye Avatar answered Nov 29 '25 15:11

Abdoul Ndiaye



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!