{"id":312003,"date":"2021-07-30T12:00:38","date_gmt":"2021-07-30T09:00:38","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-use-readonly-properties-in-php-8-1-cloudsavvy-it\/"},"modified":"2021-07-30T12:00:38","modified_gmt":"2021-07-30T09:00:38","slug":"how-to-use-readonly-properties-in-php-8-1-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-use-readonly-properties-in-php-8-1-cloudsavvy-it\/","title":{"rendered":"#How to Use Readonly Properties in PHP 8.1 \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2e47c7089ae\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2e47c7089ae\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-readonly-properties-in-php-8-1-cloudsavvy-it\/#Writing_Readonly_Properties\" >Writing Readonly Properties<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-readonly-properties-in-php-8-1-cloudsavvy-it\/#Caveats\" >Caveats<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-readonly-properties-in-php-8-1-cloudsavvy-it\/#When_to_Use_Readonly_Properties\" >When to Use Readonly Properties?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-readonly-properties-in-php-8-1-cloudsavvy-it\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Use Readonly Properties in PHP 8.1 \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage aligncenter size-full wp-image-4047\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2017\/08\/1ac500da.png?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2017\/08\/1ac500da.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2017\/08\/1ac500da.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"PHP Logo\" width=\"1400\" height=\"600\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>PHP 8.1 adds support for a <code>readonly<\/code> modifier on class properties. A property that\u2019s tagged in this way can only be set once. Trying to change a readonly property\u2019s value after initialization will throw an error.<\/p>\n<p>\u201cReadonly\u201d is quite a vague term, with varying implications in individual programming languages. In this context, \u201creadonly\u201d really means \u201cimmutable\u201d\u2014you can set a property\u2019s value, but it can\u2019t be changed afterward.<\/p>\n<h2 id=\"writing-readonly-properties\"><span class=\"ez-toc-section\" id=\"Writing_Readonly_Properties\"><\/span>Writing Readonly Properties<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Add the <code>readonly<\/code> modifier to make a property readonly. It should be placed between the property\u2019s access modifier and its typehint.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">class<\/span> Demo <span class=\"br0\">{<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> string <span class=\"re0\">$Mutable<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> readonly string <span class=\"re0\">$Immutable<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">(<\/span>\n        string <span class=\"re0\">$Mutable<\/span><span class=\"sy0\">,<\/span>\n        string <span class=\"re0\">$Immutable<\/span><span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span>\n\u00a0\n        <span class=\"re0\">$this<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">Mutable<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$Mutable<\/span><span class=\"sy0\">;<\/span>\n        <span class=\"re0\">$this<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">Immutable<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$Immutable<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p><code>$Mutable<\/code> is a regular public property. You can change its value at any time, either within class methods or from outside:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"re0\">$demo<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw2\">new<\/span> Demo<span class=\"br0\">(<\/span><span class=\"st0\">\"A\"<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">\"X\"<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"re0\">$demo<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">Mutable<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">\"B\"<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p><code>$Immutable<\/code> is a little different. You can still read its value whenever you want, but any modifications will fail with an <code>Error<\/code>:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"co1\">\/\/ Throws an Error<\/span>\n<span class=\"re0\">$demo<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">Immutable<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">\"Y\"<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The <code>readonly<\/code> modifier is also supported in promoted constructor properties:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">class<\/span> Demo <span class=\"br0\">{<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">(<\/span>\n        <span class=\"kw2\">public<\/span> readonly string <span class=\"re0\">$Immutable<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">\"foobar\"<\/span>\n    <span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span><span class=\"br0\">}<\/span>\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>You can still set the property manually in your constructor when you\u2019re using promotion with a default value. It\u2019s the <em>function parameter<\/em> that receives the default value, not the property instance. The promotion desugars to the same code shown in the earlier example.<\/p>\n<h2 id=\"caveats\"><span class=\"ez-toc-section\" id=\"Caveats\"><\/span>Caveats<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Readonly properties are a straightforward syntax enhancement that you can adopt at your leisure. There are no backward compatibility implications, and their usage is entirely optional. If you do start to add them, there are a few caveats and limitations to be aware of.<\/p>\n<p>Unlike regular properties, readonly properties aren\u2019t allowed to have a default value in their definition:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">class<\/span> Demo <span class=\"br0\">{<\/span>\n    <span class=\"kw2\">protected<\/span> readonly string <span class=\"re0\">$foo<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">\"bar\"<\/span><span class=\"sy0\">;<\/span>\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>This statement defines and initializes <code>$foo<\/code>. You can\u2019t change its value after initialization, so the property is effectively a constant. As a result, default values are banned, and you should use a real constant instead:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">class<\/span> Demo <span class=\"br0\">{<\/span>\n    <span class=\"kw2\">const<\/span> foo <span class=\"sy0\">=<\/span> <span class=\"st0\">\"bar\"<\/span><span class=\"sy0\">;<\/span>\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Following on from this restriction, <code>readonly<\/code> is <em>only<\/em> supported on typed properties. The following property has an illegal definition:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">class<\/span> Demo <span class=\"br0\">{<\/span>\n    <span class=\"kw2\">protected<\/span> readonly <span class=\"re0\">$foo<\/span><span class=\"sy0\">;<\/span>\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>An untyped property, like <code>$foo<\/code> above, has an implicit default value of <code>null<\/code>. If <code>readonly<\/code> was allowed, the \u201cno implicit constants\u201d rule would resurface. Typed properties distinguish between \u201cuninitialized\u201d and \u201cnull\u201d states, so they exist without any value until you explicitly set one.<\/p>\n<p>The readonly modifier has special rules when used as part of class inheritance. Child classes can\u2019t add or remove <code>readonly<\/code> on properties defined by their parents.<\/p>\n<p>Making a writable property <code>readonly<\/code> would break the parent class if its methods mutated the value. While removing the constraint is seemingly innocuous, the RFC views <code>readonly<\/code> as an \u201cintentional restriction\u201d of capabilities that would be lost if inheritance overrides were allowed. It\u2019s forbidden so that parent classes can assert that children can\u2019t cause side effects by modifying properties that are meant to be readonly.<\/p>\n<p>Readonly properties can only be set within the scope in which they\u2019re defined. This means that <code>public<\/code> properties can\u2019t be set from outside a class, even if they haven\u2019t been previously initialized:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">class<\/span> Demo <span class=\"br0\">{<\/span>\n    <span class=\"kw2\">public<\/span> readonly string <span class=\"re0\">$foo<\/span><span class=\"sy0\">;<\/span>\n<span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"re0\">$d<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw2\">new<\/span> Demo<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"re0\">$d<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">foo<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">\"bar\"<\/span><span class=\"sy0\">;<\/span>  <span class=\"co1\">\/\/ illegal<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Initialization must occur within the class that defines the property. As a result, readonly properties are closer to the immutable fields of other programming languages as opposed to pre-existing PHP properties.<\/p>\n<p>The <code>readonly<\/code> modifier <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>lies equally to all writes. It doesn\u2019t distinguish between internal and external access. You can\u2019t have a property that\u2019s publicly readonly but writable within the class, although a future spec extension could allow it.<\/p>\n<p>One final gotcha concerns the <code>clone<\/code> keyword. This code won\u2019t work:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">class<\/span> Demo <span class=\"br0\">{<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">(<\/span>\n        <span class=\"kw2\">public<\/span> string <span class=\"re0\">$foo<\/span>\n    <span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span><span class=\"br0\">}<\/span>\n<span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"re0\">$d<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw2\">new<\/span> Demo<span class=\"br0\">(<\/span><span class=\"st0\">\"bar\"<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"re0\">$d2<\/span> <span class=\"sy0\">=<\/span> clone <span class=\"re0\">$d<\/span><span class=\"sy0\">;<\/span>\n<span class=\"re0\">$d2<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">foo<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">\"foobar\"<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Cloning follows the same rules as regular property accesses. Even though the change to <code>foobar<\/code> is the first time that\u00a0<code>foo<\/code> is accessed on <code>$d2<\/code>, the property has already been initialized by the cloning process. There\u2019s an implicit initialization during the clone.<\/p>\n<h2 id=\"when-to-use-readonly-properties\"><span class=\"ez-toc-section\" id=\"When_to_Use_Readonly_Properties\"><\/span>When to Use Readonly Properties?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Readonly properties will significantly accelerate the creation of simple classes, which represent data structures. It\u2019s common to write throwaway classes to hold HTTP request parameters, data transfer objects, and response data. These are typically immutable, where properties aren\u2019t expected to change after the class is constructed.<\/p>\n<p>You previously had two unappealing choices when writing struct-like classes: use public properties, speeding up development but allowing modification, or spend time manually adding getter methods to expose protected properties.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"co1\">\/\/ Not ideal - Properties could be modified externally<\/span>\n<span class=\"kw2\">class<\/span> UserCreationRequest <span class=\"br0\">{<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">(<\/span>\n        <span class=\"kw2\">public<\/span> string <span class=\"re0\">$Username<\/span><span class=\"sy0\">,<\/span>\n        <span class=\"kw2\">public<\/span> string <span class=\"re0\">$Password<\/span>\n    <span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span><span class=\"br0\">}<\/span>\n<span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"co1\">\/\/ Not ideal either - Lots of boilerplate code<\/span>\n<span class=\"kw2\">class<\/span> UserCreationRequest <span class=\"br0\">{<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">(<\/span>\n        <span class=\"kw2\">protected<\/span> string <span class=\"re0\">$Username<\/span><span class=\"sy0\">,<\/span>\n        <span class=\"kw2\">protected<\/span> string <span class=\"re0\">$Password<\/span>\n    <span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span><span class=\"br0\">}<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> getUsername<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> string <span class=\"br0\">{<\/span>\n        <span class=\"kw1\">return<\/span> <span class=\"re0\">$this<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">Username<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">}<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> getPassword<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> string <span class=\"br0\">{<\/span>\n        <span class=\"kw1\">return<\/span> <span class=\"re0\">$this<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">Password<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">}<\/span>\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Readonly properties finally make the ideal approach possible:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">class<\/span> UserCreationRequest <span class=\"br0\">{<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">(<\/span>\n        <span class=\"kw2\">public<\/span> readonly string <span class=\"re0\">$Username<\/span><span class=\"sy0\">,<\/span>\n        <span class=\"kw2\">public<\/span> readonly string <span class=\"re0\">$Password<\/span>\n    <span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span><span class=\"br0\">}<\/span>\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The properties are publicly accessible but immutable. Combined with constructor property promotion, readonly properties promise to significantly slim down boilerplate code, letting you write useful classes more quickly.<\/p>\n<p><code>readonly<\/code> also aids code readability and better indicates your intentions. Anyone reading or editing the <code>UserCreationRequest<\/code> class knows that its properties aren\u2019t meant to change. In the first example, it\u2019s unknown whether other code in the project modifies the class properties directly. The second example is a little clearer, but could still induce a developer to implement redundant <code>setUsername()<\/code> and <code>setPassword()<\/code> methods.<\/p>\n<h2 id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>readonly<\/code> modifier brings built-in immutability support to PHP class properties. It makes your code clearer and prevents unintentional value changes by enforcing immutability at runtime.<\/p>\n<p>Looking back to the PHP 7 release <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/watch-movies-tv-seriess\/\" data-internallinksmanager029f6b8e52c=\"8\" title=\"Watch Movies &amp; TV Series\" target=\"_blank\" rel=\"noopener\">series<\/a>, creating a basic struct-like class used to involve defining typed properties, writing a constructor that set those properties, and then adding getter methods to expose their values. With PHP 8.1, you can condense all that down to a single constructor signature by combining <code>public readonly<\/code> and property promotion.<\/p>\n<p>Readonly properties <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/wiki.php.net\/rfc\/readonly_properties_v2\">are implemented<\/a> in the latest PHP 8.1 development builds. The production-ready release will arrive in November of 2021.\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/13044\/how-to-use-readonly-properties-in-php-8-1\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Use Readonly Properties in PHP 8.1 \u2013 CloudSavvy IT&#8221; PHP 8.1 adds support for a readonly modifier on class properties. A property that\u2019s tagged in this way can only be set once. Trying to change a readonly property\u2019s value after initialization will throw an error. \u201cReadonly\u201d is quite a vague term, with varying&#8230;<\/p>\n","protected":false},"author":1,"featured_media":312004,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2017\/08\/1ac500da.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-312003","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/312003","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=312003"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/312003\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/312004"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=312003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=312003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=312003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}