Skip to content

Core\Reflect\ClassInfo::name

$classInfo->name(): string

Returns the name of the class that a description is about.

name() returns the class name as a string, the same way the class declaration writes it. A class in a namespace keeps its namespace, so the result is Shop\Order and not Order.

When the description comes from Core\Reflect::forObject, the name is the class of the object itself. A variable with the type of a parent class can contain an object of a child class. name() then returns the name of the child class. You can give the result to Core\Reflect::forClass to get the same description again.

The examples below print the class of a few objects, show that the object decides the name and not the variable, and write a log line that names the class of each job.

Return value

Returns string. The class name, such as App\Model\User for a class in a namespace. It is never an alias that the calling code used.

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

Print the class of an object

<?nvs
// Shows how to print the name of the class of an object.
class Invoice
{
public int $number = 1042;
}
class Customer
{
public string $email = 'ada@example.com';
}
// `forObject` describes the class of an object. `name` returns the class name.
// This prints `Invoice` and `Customer`.
var $invoice = Core\Reflect::forObject(new Invoice());
echo $invoice->name(), "\n";
var $customer = Core\Reflect::forObject(new Customer());
echo $customer->name(), "\n";
Output
Invoice
Customer

The object decides the name

<?nvs
// Shows that the name is the class of the object, not the type of the variable.
class Shape
{
public function sides(): int
{
return 0;
}
}
class Triangle extends Shape
{
public function sides(): int
{
return 3;
}
}
class Square extends Shape
{
public function sides(): int
{
return 4;
}
}
array<Shape> $shapes = [new Triangle(), new Square()];
foreach ($shapes as Shape $shape) {
// The variable has the type `Shape`. This prints `Triangle`, then `Square`.
var $info = Core\Reflect::forObject($shape);
echo $info->name(), ' has ', $shape->sides(), " sides\n";
}
// `forClass` describes a class by its name. This prints `Shape`.
var $found = Core\Reflect::forClass('Shape');
if ($found != null) {
Core\Reflect\ClassInfo $parent = $found;
echo $parent->name(), "\n";
}
Output
Triangle has 3 sides
Square has 4 sides
Shape

Log the class of each job

<?nvs
// Shows how a job queue writes a log line with the class name of each job it runs.
interface Job
{
public function run(): string;
}
class SendWelcomeMail implements Job
{
public function run(): string
{
return 'mail sent';
}
}
class ResizeImage implements Job
{
public function run(): string
{
return 'image resized';
}
}
class Queue
{
public static function work(array<Job> $jobs): void
{
foreach ($jobs as Job $job) {
string $result = $job->run();
// The log line starts with the class name, so you can find the code of the job.
var $info = Core\Reflect::forObject($job);
echo '[', $info->name(), '] ', $result, "\n";
}
}
}
// This prints one log line for each job.
Queue::work([new SendWelcomeMail(), new ResizeImage()]);
Output
[SendWelcomeMail] mail sent
[ResizeImage] image resized