Content · Web Development · PHP

PHP ReflectionClass::getDefaultProperties() Method: Usage and Examples

PHP ReflectionClass::getDefaultProperties() Method: Usage and Examples: Returns declared default property values, including non-public defaults, but not uninitialized typed properties.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

ReflectionClass inspects the definition of a class, rather than running its constructor or calling its methods.

What Is ReflectionClass::getDefaultProperties in PHP?


Returns declared default property values, including non-public defaults, but not uninitialized typed properties.

ReflectionClass::getDefaultProperties()

This operation does not accept method arguments.

Example 1: Understanding the Basic Behavior


<?php

class Report { public const VERSION = 2; public string $title = 'Draft'; protected int $pages = 10; public static string $category = 'docs'; public function publish(): void {} }
$reflectionClass = new ReflectionClass(Report::class);
$result = $reflectionClass->getDefaultProperties();
var_dump($result);

Output:

array(3) {
  ["category"]=>
  string(4) "docs"
  ["title"]=>
  string(5) "Draft"
  ["pages"]=>
  int(10)
}

Explanation: Returns declared default property values, including non-public defaults, but not uninitialized typed properties.

Example 2: Testing Another Case


<?php

interface Exportable { public function export(): string; }
trait HasId { public function getId(): int { return 12; } }
class Invoice implements Exportable { use HasId; public string $name = 'Invoice'; public const STATE = 'open'; public function export(): string { return 'invoice'; } }
$reflectionClass = new ReflectionClass(Invoice::class);
$result = $reflectionClass->getDefaultProperties();
var_dump($result);

Output:

array(1) {
  ["name"]=>
  string(7) "Invoice"
}

Explanation: Uninitialized typed properties have no default value and may not appear in the resulting array.

Practical Example: Applying the Operation


<?php

abstract class BaseJob { protected string $name = 'job'; abstract public function run(): void; }
class CompletedJob extends BaseJob { public function run(): void {} }
$reflectionClass = new ReflectionClass(CompletedJob::class);
$result = $reflectionClass->getDefaultProperties();
var_dump($result);

Output:

array(1) {
  ["name"]=>
  string(3) "job"
}

Explanation: The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.

Important Behavior and Edge Cases


  • Uninitialized typed properties have no default value and may not appear in the resulting array.
  • The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.

Comparison With Related Approaches


ApproachWhen to Use It
ReflectionClass::getDefaultPropertiesReturns declared default property values, including non-public defaults, but not uninitialized typed properties.
ReflectionClass::getProperties()getDefaultProperties() reports initialized defaults as values; getProperties() returns property metadata descriptors.

Common Beginner Mistakes


MistakeCorrect Approach
Treating metadata returned by ReflectionClass::getDefaultProperties() as though it executes the reflected class member.Uninitialized typed properties have no default value and may not appear in the resulting array.
Choosing ReflectionClass::getProperties() without checking whether it provides the same result.getDefaultProperties() reports initialized defaults as values; getProperties() returns property metadata descriptors.

Key Takeaways


  • Returns declared default property values, including non-public defaults, but not uninitialized typed properties.
  • Uninitialized typed properties have no default value and may not appear in the resulting array.
  • The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.

The examples demonstrate where ReflectionClass::getDefaultProperties fits within reflection and metadata. Verify the relevant preconditions whenever you adapt the code to real data.

Share this resourceSend this page to someone who may find it useful.