PHP Reference ยท Functions and Classes
PHP get_class() and get_object_vars()
Reference for get_class(), ::class, get_called_class(), get_object_vars(), get_class_vars() and get_class_methods(): scope rules and examples.
get_class() returns the name of the class an object was created from. Its companions get_object_vars(), get_class_vars() and get_class_methods() list the properties and methods that are visible from the place where you call them.
Since PHP 5.5 the ::class constant gives the same answer without a function call: $object::class for an object, Foo::class for a name resolved at compile time.
get_class(object $object): string get_object_vars(object $object): array
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
get_class(object $object): string get_called_class(): string // like static::class get_parent_class(object|string $objectOrClass = ?): string|false get_object_vars(object $object): array get_class_vars(string $class): array get_class_methods(object|string $objectOrClass): array
| Function | Returns |
|---|---|
get_class() |
The fully qualified class name, without a leading backslash. |
get_object_vars() |
An associative array of the current values of the properties accessible in the calling scope, including dynamic ones. Uninitialized typed properties are omitted. |
get_class_vars() |
The default values of the declared, accessible properties. |
get_class_methods() |
A list of method names accessible from the calling scope. |
Usage notes
- Scope decides the result. Called from outside,
get_object_vars()returns only public properties; from inside the class it also returns protected and private ones. get_class()without arguments (meaning "the current class") is deprecated in PHP 8.3; writeself::classorstatic::class.- Prefer
::classfor names known in advance: it is resolved againstuseimports and never triggers autoloading. - Anonymous classes get a generated name starting with
class@anonymous; do not rely on its exact text. get_class_vars()shows defaults, not current values. Properties without a default show asnullonly if untyped; typed properties without default are skipped.- Dynamic properties (creating undeclared ones) are deprecated since PHP 8.2 unless the class is marked
#[AllowDynamicProperties]. - Casting to array with
(array) $objectalso exposes private and protected values, with mangled keys; use it for debugging, not for logic. - Enumerating for output: prefer an explicit
toArray()method overget_object_vars()so output does not change when you add a property.