[doc-en] master: PHP 8.6: Add ReflectionProperty::isReadable and isWritable (#6110)

From: Date: Wed, 18 Nov 2026 19:32:07 +0000
Subject: [doc-en] master: PHP 8.6: Add ReflectionProperty::isReadable and isWritable (#6110)
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-23978@lists.php.net to get a copy of this message
Author: Jordi Kroon (jordikroon) Committer: GitHub (web-flow) Pusher: Crell Date: 2026-11-18T13:32:04-06:00 Commit: https://github.com/php/doc-en/commit/0fed0090046dc4a062211dd285a3bc89e07696c2 Raw diff: https://github.com/php/doc-en/commit/0fed0090046dc4a062211dd285a3bc89e07696c2.diff PHP 8.6: Add ReflectionProperty::isReadable and isWritable (#6110) * PHP 8.6: Add ReflectionProperty::isReadable and isWritable * clarify behavior * Apply batched suggestions from code review Co-authored-by: Larry Garfield <larry@garfieldtech.com> * Apply batched suggestions from code review Co-authored-by: Larry Garfield <larry@garfieldtech.com> --------- Co-authored-by: Larry Garfield <larry@garfieldtech.com> Changed paths: A reference/reflection/reflectionproperty/isreadable.xml A reference/reflection/reflectionproperty/iswritable.xml M reference/reflection/versions.xml Diff: diff --git a/reference/reflection/reflectionproperty/isreadable.xml b/reference/reflection/reflectionproperty/isreadable.xml new file mode 100644 index 000000000000..b4229876462a --- /dev/null +++ b/reference/reflection/reflectionproperty/isreadable.xml @@ -0,0 +1,182 @@ +<?xml version="1.0" encoding="utf-8"?> +<refentry xml:id="reflectionproperty.isreadable" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"> + <refnamediv> + <refname>ReflectionProperty::isReadable</refname> + <refpurpose>Checks whether a property can be read from a given scope</refpurpose> + </refnamediv> + + <refsect1 role="description"> + &reftitle.description; + <methodsynopsis role="ReflectionProperty"> + <modifier>public</modifier> <type>bool</type><methodname>ReflectionProperty::isReadable</methodname> + <methodparam><type class="union"><type>string</type><type>null</type></type><parameter>scope</parameter></methodparam> + <methodparam choice="opt"><type class="union"><type>object</type><type>null</type></type><parameter>object</parameter><initializer>&null;</initializer></methodparam> + </methodsynopsis> + <simpara> + Checks whether reading the property from the given + <parameter>scope</parameter> would be allowed. + The visibility of the property, its + <link linkend="language.oop5.property-hooks">hooks</link>, + the <link linkend="object.get">__get()</link> and + <link linkend="object.isset">__isset()</link> magic methods and, + if an <parameter>object</parameter> is given, the state of the property + on that object are taken into account. + </simpara> + <note> + <simpara> + A return value of &true; does not guarantee that reading the property + succeeds. For example, a <literal>get</literal> hook or + <link linkend="object.get">__get()</link> may still throw an exception. + </simpara> + </note> + </refsect1> + + <refsect1 role="parameters"> + &reftitle.parameters; + <variablelist> + <varlistentry> + <term><parameter>scope</parameter></term> + <listitem> + <simpara> + The name of the class from which the property would be read, + or &null; for the global scope, that is, code outside of any class. + Use <literal>static::class</literal> to check from the scope of the + calling code, if inside a class. + </simpara> + </listitem> + </varlistentry> + <varlistentry> + <term><parameter>object</parameter></term> + <listitem> + <simpara> + The object on which the property would be read. If &null;, only the + declaration of the property is considered. + Must be &null; for static properties. + </simpara> + </listitem> + </varlistentry> + </variablelist> + </refsect1> + + <refsect1 role="returnvalues"> + &reftitle.returnvalues; + <simpara> + Returns &false; if the property is not visible from + <parameter>scope</parameter>, if it is a + <link linkend="language.oop5.property-hooks.virtual">virtual property</link> + without a <literal>get</literal> hook, or if it is uninitialized. + Otherwise &true; is returned. + </simpara> + <simpara> + However, if the class defines <link linkend="object.get">__get()</link>, + &true; is returned for a non-static property that is not visible from + <parameter>scope</parameter> or does not exist on + <parameter>object</parameter>. + If <parameter>object</parameter> is given and the class also defines + <link linkend="object.isset">__isset()</link>, the result of calling + <link linkend="object.isset">__isset()</link> is returned instead. + </simpara> + </refsect1> + + <refsect1 role="errors"> + &reftitle.errors; + <simpara> + Throws a <exceptionname>ReflectionException</exceptionname> if + <parameter>object</parameter> is given for a static property, or if + <parameter>object</parameter> is not an instance of the class the property + was declared in. + </simpara> + <simpara> + Throws an <exceptionname>Error</exceptionname> if <parameter>scope</parameter> is + not the name of an existing class. + </simpara> + </refsect1> + + <refsect1 role="examples"> + &reftitle.examples; + <example xml:id="reflectionproperty.isreadable.example.basic"> + <title><methodname>ReflectionProperty::isReadable</methodname> example</title> + <programlisting role="php"> +<![CDATA[ +<?php +class User +{ + public string $name; + private string $password = 'secret'; +} + +$user = new User(); + +$nameProperty = new ReflectionProperty(User::class, 'name'); + +// Without an object, only the declaration of the property is considered +var_dump($nameProperty->isReadable(null)); + +// The property is not initialized yet +var_dump($nameProperty->isReadable(null, $user)); + +$user->name = 'Alice'; +var_dump($nameProperty->isReadable(null, $user)); + +$passwordProperty = new ReflectionProperty(User::class, 'password'); + +// Private properties can only be read from inside the class +var_dump($passwordProperty->isReadable(null)); +var_dump($passwordProperty->isReadable(User::class)); +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +bool(true) +bool(false) +bool(true) +bool(false) +bool(true) +]]> + </screen> + </example> + </refsect1> + + <refsect1 role="notes"> + &reftitle.notes; + <note> + <simpara> + If <parameter>object</parameter> is an uninitialized + <link linkend="language.oop5.lazy-objects">lazy object</link>, + calling this method may trigger its initialization. + </simpara> + </note> + </refsect1> + + <refsect1 role="seealso"> + &reftitle.seealso; + <simplelist> + <member><methodname>ReflectionProperty::isWritable</methodname></member> + <member><methodname>ReflectionProperty::isInitialized</methodname></member> + <member><methodname>ReflectionProperty::hasHook</methodname></member> + <member><link linkend="language.oop5.overloading.members">Property overloading</link></member> + </simplelist> + </refsect1> + +</refentry> +<!-- Keep this comment at the end of the file +Local variables: +mode: sgml +sgml-omittag:t +sgml-shorttag:t +sgml-minimize-attributes:nil +sgml-always-quote-attributes:t +sgml-indent-step:1 +sgml-indent-data:t +indent-tabs-mode:nil +sgml-parent-document:nil +sgml-default-dtd-file:"~/.phpdoc/manual.ced" +sgml-exposed-tags:nil +sgml-local-catalogs:nil +sgml-local-ecat-files:nil +End: +vim600: syn=xml fen fdm=syntax fdl=2 si +vim: et tw=78 syn=sgml +vi: ts=1 sw=1 +--> diff --git a/reference/reflection/reflectionproperty/iswritable.xml b/reference/reflection/reflectionproperty/iswritable.xml new file mode 100644 index 000000000000..857148818085 --- /dev/null +++ b/reference/reflection/reflectionproperty/iswritable.xml @@ -0,0 +1,184 @@ +<?xml version="1.0" encoding="utf-8"?> +<refentry xml:id="reflectionproperty.iswritable" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"> + <refnamediv> + <refname>ReflectionProperty::isWritable</refname> + <refpurpose>Checks whether a property can be written from a given scope</refpurpose> + </refnamediv> + + <refsect1 role="description"> + &reftitle.description; + <methodsynopsis role="ReflectionProperty"> + <modifier>public</modifier> <type>bool</type><methodname>ReflectionProperty::isWritable</methodname> + <methodparam><type class="union"><type>string</type><type>null</type></type><parameter>scope</parameter></methodparam> + <methodparam choice="opt"><type class="union"><type>object</type><type>null</type></type><parameter>object</parameter><initializer>&null;</initializer></methodparam> + </methodsynopsis> + <simpara> + Checks whether writing to the property from the given + <parameter>scope</parameter> would be allowed. + The visibility of the property, including its + <link linkend="language.oop5.visibility-members-aviz">asymmetric visibility</link>, + its <link linkend="language.oop5.property-hooks">hooks</link>, + the <link linkend="object.set">__set()</link> magic method and, + if an <parameter>object</parameter> is given, the state of a + <link linkend="language.oop5.properties.readonly-properties">readonly property</link> + on that object are taken into account. + </simpara> + <note> + <simpara> + A return value of &true; does not guarantee that writing to the property + succeeds. For example, a <literal>set</literal> hook or + <link linkend="object.set">__set()</link> may still throw an exception, + and the type of the assigned value is not considered. + </simpara> + </note> + </refsect1> + + <refsect1 role="parameters"> + &reftitle.parameters; + <variablelist> + <varlistentry> + <term><parameter>scope</parameter></term> + <listitem> + <simpara> + The name of the class from which the property would be written, + or &null; for the global scope, that is, code outside of any class. + Use <literal>static::class</literal> to check from the scope of the + calling code, if inside a class. + </simpara> + </listitem> + </varlistentry> + <varlistentry> + <term><parameter>object</parameter></term> + <listitem> + <simpara> + The object on which the property would be written. If &null;, only the + declaration of the property is considered. + Must be &null; for static properties. + </simpara> + </listitem> + </varlistentry> + </variablelist> + </refsect1> + + <refsect1 role="returnvalues"> + &reftitle.returnvalues; + <simpara> + Returns &false; if the visibility or + <link linkend="language.oop5.visibility-members-aviz">set visibility</link> + of the property does not allow writing from + <parameter>scope</parameter>, if it is a + <link linkend="language.oop5.property-hooks.virtual">virtual property</link> + without a <literal>set</literal> hook, or if + <parameter>object</parameter> is given and the readonly property is + already initialized on it. + Otherwise &true; is returned. + </simpara> + <simpara> + However, if the class defines <link linkend="object.set">__set()</link>, + &true; is returned for a non-static property that is not visible from + <parameter>scope</parameter>. + </simpara> + </refsect1> + + <refsect1 role="errors"> + &reftitle.errors; + <simpara> + Throws a <exceptionname>ReflectionException</exceptionname> if + <parameter>object</parameter> is given for a static property, or if + <parameter>object</parameter> is not an instance of the class the property + was declared in. + </simpara> + <simpara> + Throws an <exceptionname>Error</exceptionname> if <parameter>scope</parameter> is + not the name of an existing class. + </simpara> + </refsect1> + + <refsect1 role="examples"> + &reftitle.examples; + <example xml:id="reflectionproperty.iswritable.example.basic"> + <title><methodname>ReflectionProperty::isWritable</methodname> example</title> + <programlisting role="php"> +<![CDATA[ +<?php +class User +{ + public readonly string $name; + + public function setName(string $name): void + { + $this->name = $name; + } +} + +$reflectionProperty = new ReflectionProperty(User::class, 'name'); +$user = new User(); + +// Readonly properties can only be written from inside the class +var_dump($reflectionProperty->isWritable(null, $user)); + +// The property is not initialized yet +var_dump($reflectionProperty->isWritable(User::class, $user)); + +$user->setName('Alice'); +var_dump($reflectionProperty->isWritable(User::class, $user)); + +// Without an object, only the declaration of the property is considered +var_dump($reflectionProperty->isWritable(User::class)); +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +bool(false) +bool(true) +bool(false) +bool(true) +]]> + </screen> + </example> + </refsect1> + + <refsect1 role="notes"> + &reftitle.notes; + <note> + <simpara> + If <parameter>object</parameter> is an uninitialized + <link linkend="language.oop5.lazy-objects">lazy object</link>, + calling this method on a readonly property may trigger its + initialization. + </simpara> + </note> + </refsect1> + + <refsect1 role="seealso"> + &reftitle.seealso; + <simplelist> + <member><methodname>ReflectionProperty::isReadable</methodname></member> + <member><methodname>ReflectionProperty::isReadOnly</methodname></member> + <member><methodname>ReflectionProperty::isPrivateSet</methodname></member> + <member><methodname>ReflectionProperty::isProtectedSet</methodname></member> + </simplelist> + </refsect1> + +</refentry> +<!-- Keep this comment at the end of the file +Local variables: +mode: sgml +sgml-omittag:t +sgml-shorttag:t +sgml-minimize-attributes:nil +sgml-always-quote-attributes:t +sgml-indent-step:1 +sgml-indent-data:t +indent-tabs-mode:nil +sgml-parent-document:nil +sgml-default-dtd-file:"~/.phpdoc/manual.ced" +sgml-exposed-tags:nil +sgml-local-catalogs:nil +sgml-local-ecat-files:nil +End: +vim600: syn=xml fen fdm=syntax fdl=2 si +vim: et tw=78 syn=sgml +vi: ts=1 sw=1 +--> diff --git a/reference/reflection/versions.xml b/reference/reflection/versions.xml index 937053851ecd..2a04a5448ac5 100644 --- a/reference/reflection/versions.xml +++ b/reference/reflection/versions.xml @@ -370,6 +370,8 @@ <function name="reflectionproperty::isprotectedset" from="PHP 8 &gt;= 8.4.0"/> <function name="reflectionproperty::isvirtual" from="PHP 8 &gt;= 8.4.0"/> <function name="reflectionproperty::setrawvalue" from="PHP 8 &gt;= 8.4.0"/> + <function name="reflectionproperty::isreadable" from="PHP 8 &gt;= 8.6.0"/> + <function name="reflectionproperty::iswritable" from="PHP 8 &gt;= 8.6.0"/> <function name="reflectionextension" from="PHP 5, PHP 7, PHP 8"/> <function name="reflectionextension::__clone" from="PHP 5, PHP 7, PHP 8"/>

« previous php.doc.cvs (#23978) next »