08: Inspect Property Read and Write Access
Overview
Section titled “Overview”Check whether a form hydrator can read or write a property from public scope. Visibility and the state of a readonly property both influence the answer.
Prerequisites
Section titled “Prerequisites”- PHP 8.6 CLI. Confirm the version with
php --version. - Review the previous chapter if you’re following the series in order.
Allow about 10 minutes for the walkthrough and exercise.
What You’ll Build
Section titled “What You’ll Build”A runnable property-access-reflection.php example.
Step 1: Run the example (~5 min)
Section titled “Step 1: Run the example (~5 min)”Run the following from the root of the Code with PHP repository.
php code/php-8-6/08-property-access-reflection/property-access-reflection.php<?php
declare(strict_types=1);
final class Article{ public string $title = 'Feature tour'; public private(set) string $slug = 'feature-tour'; public readonly int $id;
public function assignId(int $id): void { $this->id = $id; }}
$article = new Article();foreach (['title', 'slug', 'id'] as $name) { $property = new ReflectionProperty(Article::class, $name); printf("%s read=%s write=%s\n", $name, $property->isReadable(null, $article) ? 'yes' : 'no', $property->isWritable(null, $article) ? 'yes' : 'no');}
$article->assignId(7);$id = new ReflectionProperty(Article::class, 'id');echo 'Initialized id readable: ', $id->isReadable(null, $article) ? 'yes' : 'no', PHP_EOL;Expected Result
Section titled “Expected Result”title read=yes write=yesslug read=yes write=noid read=no write=noInitialized id readable: yesWhy It Works
Section titled “Why It Works”Pass null for global scope and pass the actual object to include its initialization state. A class scope can yield a different result for private setters. By default the tests account for property hooks and magic access.
Exercise
Section titled “Exercise”Check slug with Article::class as the scope and compare it with the result from public scope.
Wrap-up
Section titled “Wrap-up”Continue with Default Values for Readonly Properties.