[doc-en] master: Document isset(), empty() and unset() on hooked properties (#6080)
| From: | Asish Kumar via GitHub | Date: | Sun, 15 Nov 2026 15:49:59 +0000 |
| Subject: | [doc-en] master: Document isset(), empty() and unset() on hooked properties (#6080) | ||
| Groups: | php.doc.cvs | ||
| Request: | Send a blank email to doc-cvs+get-23961@lists.php.net to get a copy of this message | ||
Author: Asish Kumar (officialasishkumar)
Committer: GitHub (web-flow)
Pusher: lacatoire
Date: 2026-11-15T16:49:56+01:00
Commit: https://github.com/php/doc-en/commit/3a0bf5e678f1023de7cd2148f2c5a70ffdff4a59
Raw diff: https://github.com/php/doc-en/commit/3a0bf5e678f1023de7cd2148f2c5a70ffdff4a59.diff
Document isset(), empty() and unset() on hooked properties (#6080)
The property hooks page did not say how isset(), empty() and unset()
behave on a property that has hooks, which surprises users who expect
them to look at the backing value the way they do for plain typed
properties.
Add a section to the property hooks page explaining that:
- isset() and empty() call the get hook and check the value it returns,
not the backing value, so a get hook that reads an uninitialized
backing value makes them throw an Error, as does the null coalescing
operator;
- without a get hook they check the backing value directly, and on a
set-only virtual property they throw an Error;
- inside its own hooks, $this->prop refers to the backing value, so a
get hook can use isset() on it, though a default value is often
simpler;
- unset() on any property with hooks throws an Error, including from
inside the class and its hooks.
Each example was run on PHP 8.4 and 8.5 and prints the output shown.
Also add notes to the isset(), empty() and unset() pages that link to
the new section.
Changed paths:
M language/oop5/property-hooks.xml
M reference/var/functions/empty.xml
M reference/var/functions/isset.xml
M reference/var/functions/unset.xml
Diff:
diff --git a/language/oop5/property-hooks.xml b/language/oop5/property-hooks.xml
index b02c0db611a6..aedc416dfce0 100644
--- a/language/oop5/property-hooks.xml
+++ b/language/oop5/property-hooks.xml
@@ -410,6 +410,153 @@ class Person {
Only working with elements of the array requires special care.
</simpara>
</sect2>
+ <sect2 xml:id="language.oop5.property-hooks.isset-unset">
+ <title><function>isset</function>, <function>empty</function> and
<function>unset</function></title>
+ <simpara>
+ When <function>isset</function> or <function>empty</function> is used on
a
+ property that has a <literal>get</literal> hook, the hook is called and
+ the value it returns is checked instead of the backing value of the property.
+ <function>isset</function> returns &true; if that value is not &null;,
+ and <function>empty</function> returns &true; if that value is equal to
&false;.
+ This is intentional: a <literal>get</literal> hook may compute or initialize
+ the value when it is read, so code outside the hooks of the property cannot
+ observe the backing value directly.
+ </simpara>
+ <simpara>
+ As a consequence, if the <literal>get</literal> hook reads a backing value
+ that has not been initialized, using <function>isset</function> or
+ <function>empty</function> on the property throws an
<exceptionname>Error</exceptionname>,
+ just like reading the property does.
+ The same applies to the
+ <link linkend="language.operators.comparison.coalesce">null coalescing
operator</link>.
+ </simpara>
+ <example>
+ <title><function>isset</function> on a property with a
<literal>get</literal> hook</title>
+ <programlisting role="php">
+<![CDATA[
+<?php
+class Article
+{
+ public ?string $title = null {
+ get => $this->title ?? 'Untitled';
+ }
+
+ public string $author {
+ get => ucfirst($this->author);
+ set => $this->author = $value;
+ }
+}
+
+$article = new Article();
+
+// The backing value is null, but the get hook returns 'Untitled'.
+var_dump(isset($article->title));
+
+// The get hook reads an uninitialized backing value.
+try {
+ var_dump(isset($article->author));
+} catch (Error $e) {
+ echo $e->getMessage(), "\n";
+}
+?>
+]]>
+ </programlisting>
+ &example.outputs;
+ <screen>
+<![CDATA[
+bool(true)
+Typed property Article::$author must not be accessed before initialization
+]]>
+ </screen>
+ </example>
+ <simpara>
+ If a backed property has no <literal>get</literal> hook,
+ <function>isset</function> and <function>empty</function> check the
backing
+ value directly, as they do for a property without hooks.
+ On a virtual property that has no <literal>get</literal> hook,
+ they throw an <exceptionname>Error</exceptionname>, because the property cannot be
read.
+ </simpara>
+ <simpara>
+ Inside the hooks of a property, <code>$this->propName</code> refers to the
+ backing value of that property, so <code>isset($this->propName)</code> checks
+ the backing value directly.
+ A <literal>get</literal> hook can use this to handle an uninitialized backing value.
+ Often, it is simpler to give the property a default value, such as &null;.
+ </simpara>
+ <example>
+ <title>Handling an uninitialized backing value</title>
+ <programlisting role="php">
+<![CDATA[
+<?php
+class Article
+{
+ // Initializing the backing value avoids the error.
+ public ?string $subtitle = null {
+ get => $this->subtitle !== null ? ucfirst($this->subtitle) : null;
+ }
+
+ // The get hook checks its own backing value.
+ public string $author {
+ get => isset($this->author) ? ucfirst($this->author) : 'Anonymous';
+ set => $this->author = $value;
+ }
+}
+
+$article = new Article();
+
+var_dump(isset($article->subtitle));
+var_dump(isset($article->author));
+var_dump($article->author);
+?>
+]]>
+ </programlisting>
+ &example.outputs;
+ <screen>
+<![CDATA[
+bool(false)
+bool(true)
+string(9) "Anonymous"
+]]>
+ </screen>
+ </example>
+ <simpara>
+ A property that has hooks cannot be unset.
+ Using <function>unset</function> on it throws an
<exceptionname>Error</exceptionname>,
+ whether the property is backed or virtual, and even when
+ <function>unset</function> is called from inside the class or one of its hooks.
+ </simpara>
+ <example>
+ <title><function>unset</function> on a property with hooks</title>
+ <programlisting role="php">
+<![CDATA[
+<?php
+class Rectangle
+{
+ public int $area {
+ get => $this->h * $this->w;
+ }
+
+ public function __construct(public int $h, public int $w) {}
+}
+
+$r = new Rectangle(4, 5);
+
+try {
+ unset($r->area);
+} catch (Error $e) {
+ echo $e->getMessage(), "\n";
+}
+?>
+]]>
+ </programlisting>
+ &example.outputs;
+ <screen>
+<![CDATA[
+Cannot unset hooked property Rectangle::$area
+]]>
+ </screen>
+ </example>
+ </sect2>
<sect2>
<title>Inheritance</title>
<sect3>
diff --git a/reference/var/functions/empty.xml b/reference/var/functions/empty.xml
index 321b43f9f8ff..6955b1a822bc 100644
--- a/reference/var/functions/empty.xml
+++ b/reference/var/functions/empty.xml
@@ -139,6 +139,15 @@ bool(false)
overloading method will be called, if declared.
</para>
</note>
+ <note>
+ <simpara>
+ When using <function>empty</function> on a property that has a
+ <literal>get</literal> hook, the hook is called and the value it returns
+ is checked. See
+ <link linkend="language.oop5.property-hooks.isset-unset">property
hooks</link>
+ for details.
+ </simpara>
+ </note>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
diff --git a/reference/var/functions/isset.xml b/reference/var/functions/isset.xml
index 459854d76c59..f40fe65710c9 100644
--- a/reference/var/functions/isset.xml
+++ b/reference/var/functions/isset.xml
@@ -180,6 +180,15 @@ bool(false)
overloading method will be called, if declared.
</para>
</note>
+ <note>
+ <simpara>
+ When using <function>isset</function> on a property that has a
+ <literal>get</literal> hook, the hook is called and the value it returns
+ is checked. See
+ <link linkend="language.oop5.property-hooks.isset-unset">property
hooks</link>
+ for details.
+ </simpara>
+ </note>
</refsect1>
<refsect1 role="seealso">
diff --git a/reference/var/functions/unset.xml b/reference/var/functions/unset.xml
index 922f9cd3c1f1..d875df0efba5 100644
--- a/reference/var/functions/unset.xml
+++ b/reference/var/functions/unset.xml
@@ -205,6 +205,14 @@ unset($foo1, $foo2, $foo3);
overloading method will be called, if declared.
</para>
</note>
+ <note>
+ <simpara>
+ Using <function>unset</function> on a property that has hooks throws an
+ <exceptionname>Error</exceptionname>. See
+ <link linkend="language.oop5.property-hooks.isset-unset">property
hooks</link>
+ for details.
+ </simpara>
+ </note>
</refsect1>
<refsect1 role="seealso">