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]];
}
[PHP 4, PHP 5, PHP 7, PHP 8]
get_class — Returns the name of the class of an object
Description
get_class[object $object
= ?]: string
Parameters
object
The tested object. This parameter may be omitted when inside a class.
Note: Explicitly passing
null
as theobject
is no longer allowed as of PHP 7.2.0 and emits anE_WARNING
. As of PHP 8.0.0, a TypeError is emitted whennull
is used.
Return Values
Returns the name of the class of which object
is an instance.
If object
is omitted when inside a class, the name of that class is returned.
If the object
is an instance of a class which exists in a namespace, the qualified namespaced name of that class is returned.
Errors/Exceptions
If get_class[] is called
with anything other than an object, TypeError is raised. Prior to PHP 8.0.0, an E_WARNING
level error was raised.
If get_class[] is called with no arguments from outside a class, Error is raised. Prior to PHP 8.0.0, an E_WARNING
level error was
raised.
Changelog
8.0.0 | Calling this function from outside a class, without any arguments, will trigger an Error. Previously, an E_WARNING was raised and the function returned false .
|
7.2.0 | Prior to this version the default value for object was null and it had the same effect as not passing any value. Now null has been removed as the default value for object , and is no longer a valid input.
|
Examples
Example #1 Using get_class[]
The above example will output:
Its name is foo My name is foo
Example #2 Using get_class[] in superclass
The above example will output:
string[3] "foo" string[3] "bar"
Example #3 Using get_class[] with namespaced classes
The above example will output:
See Also
- get_called_class[] - The "Late Static Binding" class name
- get_parent_class[] - Retrieves the parent class name for object or class
- gettype[] - Get the type of a variable
- get_debug_type[] - Gets the type name of a variable in a way that is suitable for debugging
- is_subclass_of[] - Checks if the object has this class as one of its parents or implements it
jjanak at webperfection dot net ¶
8 years ago
>= 5.5
::class
fully qualified class name, instead of get_class
--doMethod--
--doGetClassThis--
--doGetClass--
OUTPUT:
Foo::doMethod
Foo::doMethod
Quux::doMethod
Foo::doThat
Bar::doThat
Quux::doThat
Foo::doThat
Foo::doThat
Quux::doThat
ovidiu.bute [at] gmail.com ¶
12 years ago
If you are using namespaces this function will return the name of the class including the namespace, so watch out if your code does any checks for this. Ex:
namespace Shop;
emmanuel dot antico at gmail dot com ¶
9 years ago
/**
* Obtains an object class name without namespaces
*/
function get_real_class[$obj] {
$classname = get_class[$obj];
if [preg_match['@\\\\[[\w]+]$@', $classname, $matches]] {
$classname = $matches[1];
}
return $classname;
}
macnimble at gmail dot com ¶
10 years ago
Need a quick way to parse the name of a class when it's namespaced? Try this: