• 7 min read

PHP 8.6 Reflection: isReadable(), isWritable() and Parameter DocComments

PHP 8.6 adds ReflectionProperty::isReadable(), isWritable() and ReflectionParameter::getDocComment(). See working code for hydrators and tool docs.

Featured image for "PHP 8.6 Reflection: isReadable(), isWritable() and Parameter DocComments"

If you maintain a hydrator, a serializer, a form library or a DI container, you have written this check before: if ($property->isPublic()). For years that was a decent proxy for “I can read and write this property from outside the class.” It stopped being one the moment PHP got readonly properties, asymmetric visibility and property hooks.

PHP 8.6, released on November 19, 2026, adds a direct answer. ReflectionProperty::isReadable() and ReflectionProperty::isWritable() tell you whether an operation would be allowed from a given scope. The same release lets parameters carry their own DocComments, readable through a new ReflectionParameter::getDocComment(). This post covers both, with code you can adapt for tooling.

Why isPublic() stopped being enough

The isReadable/isWritable RFC by Ilija Tovilo and Larry Garfield explains the gap. isPublic() only reports that a “public” flag is set. Before PHP 8.1 that implied the property was also writable from outside. Then PHP 8.1 added readonly properties, which behave as if they were private-set. PHP 8.4 added explicit asymmetric visibility such as public private(set). The RFC says the result is that “there is currently no straightforward way to determine at runtime if reading from or writing to a property would be allowed.”

The RFC passed 27 to 0, with 3 abstentions, and is marked Implemented in PHP 8.6.

The new signatures

Both methods take the same arguments:

public function isReadable(?string $scope, ?object $object = null): bool {}
public function isWritable(?string $scope, ?object $object = null): bool {}

The $scope argument answers the question “if I tried this from here, would it work?” Pass null for the global scope, or a class name to ask from inside that class. The optional $object adds instance state to the analysis. Without it, only the declaration is considered. With it, things like “has this readonly property already been written” matter.

The manual page for isReadable() gives this example, which shows how the object argument changes the answer:

<?php
class User
{
    public string $name;
    private string $password = 'secret';
}

$user = new User();
$nameProperty = new ReflectionProperty(User::class, 'name');

var_dump($nameProperty->isReadable(null));         // true, declaration only
var_dump($nameProperty->isReadable(null, $user));  // false, not initialized yet

$user->name = 'Alice';
var_dump($nameProperty->isReadable(null, $user));  // true

$passwordProperty = new ReflectionProperty(User::class, 'password');
var_dump($passwordProperty->isReadable(null));            // false
var_dump($passwordProperty->isReadable(User::class));     // true

The uninitialized case is the one that used to bite. Reading a typed property before it is set throws an Error, and isPublic() told you nothing about that.

A safer hydrator

Here is a small hydrator for something like a PHPTek.tv attendee import. It copies array values onto an object, and it reports what it skipped instead of throwing midway through.

<?php
final class Attendee
{
    public function __construct(
        public readonly int $id,
        public private(set) string $status = 'pending',
        public string $name = '',
        public string $company = '',
    ) {}
}

/**
 * @return list<string> names that could not be assigned
 */
function fill(object $target, array $data): array
{
    $class = new ReflectionClass($target);
    $skipped = [];

    foreach ($data as $name => $value) {
        if (! $class->hasProperty($name)) {
            $skipped[] = $name;
            continue;
        }

        $property = $class->getProperty($name);

        if (! $property->isWritable(null, $target)) {
            $skipped[] = $name;
            continue;
        }

        $property->setValue($target, $value);
    }

    return $skipped;
}

$attendee = new Attendee(id: 42);

$skipped = fill($attendee, [
    'id'      => 99,
    'status'  => 'confirmed',
    'name'    => 'Ada',
    'company' => 'PHP Architect',
    'badge'   => 'VIP',
]);

print_r($skipped);
Array
(
    [0] => id
    [1] => status
    [2] => badge
)

id is skipped because it is an initialized readonly property. status is skipped because the setter is private and we asked from the global scope. badge does not exist. name and company are assigned. If you wanted a class to fill its own private-set properties, you would pass Attendee::class as the scope instead of null.

Before 8.6 you would have combined isReadOnly(), isPrivateSet(), isProtectedSet(), isInitialized() and isPublic() yourself, and likely missed a case.

What the methods do with hooks and magic methods

The RFC spells out several rules worth knowing before you rely on the result:

  • A virtual property (hooks, no backing value) is readable only if it has a get hook, and writable only if it has a set hook.
  • If a read would fall through to __get() and the class also defines __isset(), the result of __isset() is returned. With no __isset(), the property is treated as readable.
  • If a write would fall through to __set(), the property is treated as writable.
  • Static properties are supported, but they do not check magic methods, and passing an object for a static property is an error.

The manual also warns that a true result is not a guarantee. A get hook or __get() can still throw. And if you pass an uninitialized lazy object, the manual notes the call “may trigger its initialization,” which matters if you are building tooling around lazy objects.

The RFC adds one more gap: some built-in classes have effectively immutable properties without using readonly or private(set). Reflection cannot see those until the engine classes are updated.

DocComments on parameters

The second feature is smaller but handy. The Parameter DocComments RFC by Christian Schneider passed 23 to 0, with 4 abstentions. It lets you attach a DocComment to each parameter and read it with ReflectionParameter::getDocComment(), which returns string|false.

The motivation is duplication. With @param, you state the name and type twice, and the comment can drift from the signature. The new form puts the description next to the parameter:

<?php
class Schedule
{
    /**
     * Returns up to $limit sessions matching the query.
     */
    public function search(
        /** Terms to search for */
        string $query,

        /** Maximum number of results */
        int $limit = 10,
    ): array {
        return [];
    }
}

The RFC also allows the comment after the parameter, before the comma:

public function search(
    string $query /** Terms to search for */,
    int $limit = 10 /** Maximum number of results */,
): array {
    return [];
}

Putting it to work: tool descriptions

A practical use is generating descriptions for command-line help or for tool definitions you hand to an LLM. Those need a short human sentence per argument, and now you can read it straight from the signature:

<?php
/**
 * @return array<string, array{type: string, description: ?string}>
 */
function describeParameters(Closure|string $function): array
{
    $reflection = new ReflectionFunction($function);
    $result = [];

    foreach ($reflection->getParameters() as $parameter) {
        $doc = $parameter->getDocComment();

        $result[$parameter->getName()] = [
            'type' => (string) $parameter->getType(),
            'description' => $doc === false
                ? null
                : preg_replace('#^/\*\*\s*|\s*\*/$#', '', $doc),
        ];
    }

    return $result;
}

function findSessions(
    /** Terms to search for */
    string $query,

    /** Maximum number of results */
    int $limit = 10,
): array {
    return [];
}

print_r(describeParameters('findSessions'));
Array
(
    [query] => Array
        (
            [type] => string
            [description] => Terms to search for
        )

    [limit] => Array
        (
            [type] => int
            [description] => Maximum number of results
        )

)

This pairs well with the built-in types, so you no longer need a docblock parser just to get one line per argument. If you use PHPStan, Psalm or phpDocumentor, check their release notes before changing your conventions. The RFC itself says the impact on code style and formatter configuration “has to be decided in the respective communities,” so do not assume your tools read the new comments yet.

Upgrade notes

Both features are additive. The RFCs list no backward incompatible changes. Because they are new methods, guard any library code that has to run on older versions:

if (method_exists(ReflectionProperty::class, 'isWritable')) {
    // PHP 8.6+
} else {
    // fall back to isPublic() and friends
}

If you already ship a hydrator or serializer, the useful first step is to find every place that checks isPublic() or isReadOnly() as a stand-in for “can I write this,” and see whether isWritable() with the right scope gives a more accurate answer.

Sources