Skip to content

Core\Reflect::forObject

Describes the class of an object you already have.

Core\Reflect::forObject takes any value and returns a Core\Reflect\ClassInfo for the class of that object. The description has the class's name, its properties, its methods, its constants and its attributes. It describes the object's own class, so a subclass gives the subclass's name.

The parameter is mixed, so the compiler does not check that the value is an object. When it is not an object, forObject throws a RuntimeError. Check the value with Core\Reflect::typeOf first if it may be something else.

The examples below show the class name of an object, the properties of a subclass, and a log line that names the class of any value a function receives.

Parameters

  • $objectmixedThe value to describe. Reflection is for a value whose class is not known at compile time, so the parameter is mixed.

Return value

Returns Core\Reflect\ClassInfo. A Core\Reflect\ClassInfo for the object's own class. It describes the class and does not contain the object.

Errors

  • RuntimeError$object is not an object. A mixed value may be anything, so this method checks it and the caller does not need to.

Changelog

VersionChange
0.0.6The server now builds the description of each class once, when it loads your program. Core\Reflect::forClass, Core\Reflect::forObject and Core\Reflect\EnumInfo::of return that description, so two calls for the same class return the same object. A list such as properties() or methods() is built once per request.

Examples

The class of an object

<?nvs
// Shows how to get the class name of an object.
class Parcel
{
public float $weight = 2.5;
}
Parcel $parcel = new Parcel();
// This prints `Parcel`.
echo Core\Reflect::forObject($parcel)->name(), "\n";
Output
Parcel

A subclass and its properties

<?nvs
// Shows that `forObject` describes the object's own class, also when a variable has the parent type.
class Animal
{
public string $name = 'Rex';
}
class Dog extends Animal
{
public string $breed = 'Collie';
}
class Describe
{
public static function print(Animal $animal): void
{
var $info = Core\Reflect::forObject($animal);
echo $info->name(), ': ', Core\Str::join($info->readableProperties(), ', '), "\n";
}
}
// The parameter type is `Animal`. For a `Dog`, the description is the one of `Dog`.
// This prints `Animal: name` and then `Dog: name, breed`.
Describe::print(new Animal());
Describe::print(new Dog());
Output
Animal: name
Dog: name, breed

A log line for any value

<?nvs
// Shows a log line that names the class of a value, as a job queue does for a job it cannot run.
class SendEmail
{
}
class ResizeImage
{
}
class Log
{
public static function rejected(mixed $job): void
{
// `forObject` throws an error for a value that is not an object,
// so the value is checked with `typeOf` first.
if (Core\Reflect::typeOf($job) != Core\Reflect::TypeKind::Object) {
echo "rejected: not a job\n";
return;
}
echo 'rejected: ', Core\Reflect::forObject($job)->name(), "\n";
}
}
// This prints `rejected: SendEmail`, `rejected: ResizeImage` and `rejected: not a job`.
Log::rejected(new SendEmail());
Log::rejected(new ResizeImage());
Log::rejected('send-email');
Output
rejected: SendEmail
rejected: ResizeImage
rejected: not a job