Core\Reflect::forClass
Core\Reflect::forClass(string $name): ?Core\Reflect\ClassInfoCore\Reflect::forClass( string $name, ): ?Core\Reflect\ClassInfo
Describes a class when you have only its name as a string.
Core\Reflect::forClass takes a class name, such as "Shop\Order", and returns a Core\Reflect\ClassInfo. That description has the class's name, its properties, its methods, its constants and its attributes. When the running program has no class of that name, the result is null. So you can use the same call to check whether a class exists.
Write the name the way the class declaration writes it, with its namespace and without a leading backslash. The classes of Core itself are not described: the result for them is null.
The examples below show a class that exists and one that does not, a check of class names read from a setting, and a header line built from a class's public properties.
Parameters
$namestringThe class's name as its declaration writes it, with the namespace and with no\at the start.Core\Reflect\ClassInfo::namereturns this name. The text cannot betainted. Check text from a request withCore\Taint::assertTrustedfirst.
Return value
Returns ?Core\Reflect\ClassInfo. A description of that class. The result is null if the running program declares no class with that name. This is not an error.
Changelog
| Version | Change |
|---|---|
0.0.6 | The 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
A class that exists
<?nvs// Shows how to describe a class by its name, and what happens when no class has that name.
class Invoice{ public int $number = 0; public string $customer = '';}
// `Invoice` exists, so the result is a description. This prints `Invoice`.var $info = Core\Reflect::forClass('Invoice');if ($info != null) { echo $info->name(), "\n";}
// No class is called `Receipt`, so the result is `null`. This prints `no such class`.if (Core\Reflect::forClass('Receipt') == null) { echo "no such class\n";}Invoiceno such classCheck names from a setting
<?nvs// Shows how to check a list of class names, for example names read from a setting.
class CsvExport{}
class PdfExport{}
// The third name has a typing error, so `forClass` returns `null` for it.array<string> $configured = ['CsvExport', 'PdfExport', 'XmlExprot'];foreach ($configured as string $name) { if (Core\Reflect::forClass($name) == null) { echo $name, ": not found\n"; } else { echo $name, ": ok\n"; }}CsvExport: okPdfExport: okXmlExprot: not foundA header from public properties
<?nvs// Shows how to build the header line of a CSV file from the public properties of a class.
class Customer{ public int $id = 0; public string $name = ''; public string $email = ''; private string $passwordHash = '';}
class Header{ public static function of(string $className): string { var $info = Core\Reflect::forClass($className); if ($info == null) { return ''; } // `readableProperties` returns only the properties this code may read. // `passwordHash` is private, so it is not in the list. return Core\Str::join($info->readableProperties(), ','); }}
// This prints `id,name,email`.echo Header::of('Customer'), "\n";id,name,email