Skip to content

Core\Reflect\PropertyInfo::name

$propertyInfo->name(): string

Returns the name of one property of a class.

name() returns the name that the class declares, such as email or total. The name has no $ and no class name. A class also lists the properties it inherits from its parent class, and those come first.

You can pass the name to Core\Reflect\ClassInfo::hasProperty to check that a property exists. You can pass it to Core\Reflect\ClassInfo::get and Core\Reflect\ClassInfo::set to read and write the property. Names are case-sensitive, so Email does not find email.

The examples below print the properties of a class, show the properties a class inherits, and write the header line of a CSV file from the properties of a class.

Return value

Returns string. The name as the class declares it, with no $ and no class name. You can pass the name to Core\Reflect\ClassInfo::hasProperty, Core\Reflect\ClassInfo::get and Core\Reflect\ClassInfo::set.

Examples

List the properties of a class

<?nvs
// Shows how to print the names of the properties of a class.
class Customer
{
public string $email = 'ada@example.com';
public int $orders = 3;
private string $password = 'secret';
}
// This prints `email`, `orders` and `password`, in the order the class declares them.
foreach (Core\Reflect::forObject(new Customer())->properties() as Core\Reflect\PropertyInfo $property) {
echo $property->name(), "\n";
}
Output
email
orders
password

Inherited properties come first

<?nvs
// Shows that a class also lists the properties of its parent class.
class Entity
{
public int $id = 1;
public string $createdAt = '2026-01-05';
}
class Invoice extends Entity
{
public float $total = 49.5;
}
// The properties of `Entity` come first. This prints `id`, `createdAt` and `total`.
foreach (Core\Reflect::forObject(new Invoice())->properties() as Core\Reflect\PropertyInfo $property) {
echo $property->name(), "\n";
}
// You can pass a name to `hasProperty`. Names are case-sensitive, so this prints `yes` and then `no`.
var $info = Core\Reflect::forObject(new Invoice());
echo $info->hasProperty('total') ? 'yes' : 'no', "\n";
echo $info->hasProperty('Total') ? 'yes' : 'no', "\n";
Output
id
createdAt
total
yes
no

Write a csv header

<?nvs
// Writes the header line of a CSV export from the properties of a class.
class Product
{
public string $sku = 'A-100';
public string $title = 'Desk lamp';
public float $price = 24.9;
public int $stock = 12;
}
// The loop adds each name to the line, with a comma between two names.
string $header = '';
foreach (Core\Reflect::forObject(new Product())->properties() as Core\Reflect\PropertyInfo $property) {
if ($header != '') {
$header = $header . ',';
}
$header = $header . $property->name();
}
// This prints `sku,title,price,stock`.
echo $header, "\n";
Output
sku,title,price,stock