<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: shadowtje1990</title>
    <description>The latest articles on Forem by shadowtje1990 (@shadowtje1990).</description>
    <link>https://forem.com/shadowtje1990</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1057558%2Fed2e2b81-bb32-483a-a7fe-f85fe60941a1.png</url>
      <title>Forem: shadowtje1990</title>
      <link>https://forem.com/shadowtje1990</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/shadowtje1990"/>
    <language>en</language>
    <item>
      <title>How to Use Data Transfer Objects (DTOs) for Clean PHP Code</title>
      <dc:creator>shadowtje1990</dc:creator>
      <pubDate>Tue, 02 May 2023 22:37:21 +0000</pubDate>
      <link>https://forem.com/shadowtje1990/how-to-use-data-transfer-objects-dtos-for-clean-php-code-1mb8</link>
      <guid>https://forem.com/shadowtje1990/how-to-use-data-transfer-objects-dtos-for-clean-php-code-1mb8</guid>
      <description>&lt;p&gt;In modern web applications, it is common to have different layers of an application, such as the database layer, the business logic layer, and the presentation layer. Each layer is responsible for a specific task and communicates with the other layers through interfaces. However, when transferring data between these layers, it can be difficult to manage the data and ensure that it is transferred correctly. That’s where Data Transfer Objects (DTOs) come in.&lt;/p&gt;

&lt;p&gt;A DTO is a design pattern used to transfer data between layers of an application. A DTO is an object that carries data between processes, such as between a web application and a database. It is a simple container for data that does not have any business logic, validation, or other operations.&lt;/p&gt;

&lt;p&gt;One of the most common uses of DTOs is to transfer data between layers of an application. For example, when retrieving data from a database, the data may be returned as an array or a database result set. However, the business logic layer may require the data to be in a specific format or type. A DTO can be used to convert the data from one format to another and ensure that it is passed correctly to the next layer.&lt;/p&gt;

&lt;p&gt;Another use of DTOs is to define the structure of the response objects returned by an API. When designing an API, it is important to define the structure of the response objects to ensure that they are consistent and easy to consume by client applications. A DTO can be used to define the structure of the response object and ensure that it contains all the necessary data.&lt;/p&gt;

&lt;p&gt;Now that you have a good understanding of what Data Transfer Objects (DTOs) are and why they are useful in PHP applications, it’s time to dive into some practical examples. While the theory behind DTOs is important to understand, it can be a bit dry without real-world context.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. UserDTO created through static method&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class UserDTO {
        private int $id;
        private string $name;
        private string $email;

        private function __construct(int $id, string $name, string $email) {
            $this-&amp;gt;id = $id;
            $this-&amp;gt;name = $name;
            $this-&amp;gt;email = $email;
        }

        public static function create(int $id, string $name, string $email): UserDTO {
            return new self($id, $name, $email);
        }

        public function id(): int {
            return $this-&amp;gt;id;
        }

        public function name(): string {
            return $this-&amp;gt;name;
        }

        public function email(): string {
            return $this-&amp;gt;email;
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;em&gt;UserDTO&lt;/em&gt; class has three private properties: &lt;em&gt;$id&lt;/em&gt;, &lt;em&gt;$name&lt;/em&gt;, and &lt;em&gt;$email&lt;/em&gt;. The constructor is made private to ensure that the class can only be instantiated using the static &lt;em&gt;create&lt;/em&gt; method.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;create&lt;/em&gt; method accepts the three properties as arguments and returns a new &lt;em&gt;UserDTO&lt;/em&gt; object with those properties. The &lt;em&gt;id&lt;/em&gt;, &lt;em&gt;name&lt;/em&gt;, and &lt;em&gt;email&lt;/em&gt; methods provide read-only access to the private properties of the class.&lt;/p&gt;

&lt;p&gt;Using a private constructor and a static method to create the class helps to ensure that the data within the DTO is valid and can be used as intended, while preventing any external manipulation of the data.&lt;/p&gt;

&lt;p&gt;Using private properties and getter methods, is more traditional and can be used in earlier versions of PHP. It allows you to define the properties separately from the constructor, which can make the code more modular and easier to test. It also provides better encapsulation, since the properties are private and can only be accessed through the getter methods. However, this approach requires more code to implement, and may be less concise and readable than the &lt;em&gt;public readonly&lt;/em&gt; approach.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. UserDTO using public readonly&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class UserDTO {
        private function __construct(
            public readonly int $id,
            public readonly string $name,
            public readonly string $email
        ) {}

        public static function create(int $id, string $name, string $email): UserDTO {
            return new self($id, $name, $email);
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach, using &lt;em&gt;public readonly&lt;/em&gt; properties, is only available in PHP 8.0 and later. It allows you to define the properties directly within the constructor signature, which can make the code more concise and easier to read. It also provides immutability out-of-the-box, since the properties are both public and readonly. However, this approach does not provide any methods for accessing the property values, which can be a disadvantage in some situations.&lt;/p&gt;

&lt;p&gt;In summary, both approaches have their advantages and disadvantages, and the choice between them depends on the specific needs of your application. If you are using PHP 8.0 or later and require immutability, the &lt;em&gt;public readonly&lt;/em&gt; approach may be a good choice. Otherwise, the private properties and getter methods approach provides better encapsulation and can be used in earlier versions of PHP.&lt;/p&gt;

&lt;p&gt;As a bonus, if you are interested in how this works using traits. Please checkout my other article.&lt;br&gt;
&lt;a href="https://dev.to/shadowtje1990/creating-a-dto-with-traits-in-php-4fjn"&gt;&lt;strong&gt;Creating a DTO with Traits in PHP&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Creating a DTO with Traits in PHP</title>
      <dc:creator>shadowtje1990</dc:creator>
      <pubDate>Tue, 02 May 2023 22:30:44 +0000</pubDate>
      <link>https://forem.com/shadowtje1990/creating-a-dto-with-traits-in-php-4fjn</link>
      <guid>https://forem.com/shadowtje1990/creating-a-dto-with-traits-in-php-4fjn</guid>
      <description>&lt;p&gt;Data Transfer Objects (DTOs) are an essential part of any application that deals with complex data structures. They help to encapsulate data, making it easier to manipulate and pass around between different parts of your codebase. However, creating DTOs from scratch can be a tedious and repetitive process, especially if you have many DTOs to create. Fortunately, PHP’s support for traits can help make the process much easier and less repetitive.&lt;/p&gt;

&lt;p&gt;In this article, we’ll walk through an example of creating a UserDTO class using two traits — StaticCreateSelf and ToArray — that will help streamline the process of creating and using DTOs in your PHP applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Traits in PHP?
&lt;/h2&gt;

&lt;p&gt;Before we dive into the example, let’s quickly review what traits are in PHP. Traits are a way to reuse code in multiple classes without having to use inheritance. They allow you to define methods that can be used in multiple classes, without having to create a parent class that each class inherits from. This can be especially useful when you need to share functionality between classes that don’t have a natural parent-child relationship.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example: Creating a UserDTO with Traits
&lt;/h2&gt;

&lt;p&gt;In this example, we’ll create a UserDTO class using two traits: StaticCreateSelf and ToArray. The StaticCreateSelf trait will provide a static &lt;em&gt;create() _method that can be used to create a new UserDTO from an array of values. The ToArray trait will provide a _toArray()&lt;/em&gt; method that will convert a UserDTO object into an associative array.&lt;/p&gt;

&lt;p&gt;Here’s what the UserDTO class will look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    &amp;lt;?php

    declare(strict_types=1);

    namespace App\DTO;

    use App\Shared\Traits\StaticCreateSelf;
    use App\Shared\Traits\ToArray;

    class UserDTO
    {
        use StaticCreateSelf;
        use ToArray;

        public int $id;
        public string $name = '';
        public string $email = '';
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, we’ve defined the UserDTO class to have three public properties: &lt;em&gt;id&lt;/em&gt;, &lt;em&gt;name&lt;/em&gt;, and &lt;em&gt;email&lt;/em&gt;. We've also included the StaticCreateSelf and ToArray traits using the &lt;em&gt;use&lt;/em&gt; keyword.&lt;/p&gt;

&lt;p&gt;Let’s take a closer look at the StaticCreateSelf trait first. Here’s what it looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    &amp;lt;?php

    declare(strict_types=1);

    namespace App\Shared\Traits;

    trait StaticCreateSelf
    {
        public static function create(array $values): self
        {
            $dto = new self();

            foreach ($values as $key =&amp;gt; $value) {
                if (property_exists($dto, $key)) {
                    $dto-&amp;gt;$key = $value;
                }
            }

            return $dto;
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The StaticCreateSelf trait provides a &lt;em&gt;create()&lt;/em&gt; method that takes an associative array of values and returns a new instance of the class (in this case, a UserDTO). The method uses the &lt;em&gt;new self()&lt;/em&gt; expression to create a new instance of the class. It then iterates over the array of values and sets the corresponding properties on the new instance of the class.&lt;/p&gt;

&lt;p&gt;The ToArray trait is even simpler. Here’s what it looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;?php

    declare(strict_types=1);

    namespace App\Shared\Traits;

    trait ToArray
    {
        public function toArray(): array
        {
            return get_object_vars($this);
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The ToArray trait provides a &lt;em&gt;toArray()&lt;/em&gt; method that returns an associative array representation of the object. It does this by calling the &lt;em&gt;get_object_vars()&lt;/em&gt; function, which returns an associative array of all the object's properties and their values.&lt;/p&gt;

&lt;h2&gt;
  
  
  The &lt;em&gt;UserDTO&lt;/em&gt; Class
&lt;/h2&gt;

&lt;p&gt;Now that we have defined the necessary traits, we can use them to create a &lt;em&gt;UserDTO&lt;/em&gt; class. The &lt;em&gt;UserDTO&lt;/em&gt; class will define the properties of the DTO and use the &lt;em&gt;ToArray&lt;/em&gt; and &lt;em&gt;StaticCreateSelf&lt;/em&gt; traits to provide the necessary functionality. Here is the implementation of the &lt;em&gt;UserDTO&lt;/em&gt; class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

    declare(strict_types=1);

    namespace App\DTO;

    use App\Shared\Traits\StaticCreateSelf;
    use App\Shared\Traits\ToArray;

    class UserDTO
    {
        use StaticCreateSelf;
        use ToArray;

        public int $id;
        public string $name = '';
        public string $email = '';
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, the &lt;em&gt;UserDTO&lt;/em&gt; class uses the &lt;em&gt;StaticCreateSelf&lt;/em&gt; and &lt;em&gt;ToArray&lt;/em&gt; traits to provide the necessary functionality. The properties of the DTO are defined as public properties, which can be accessed and modified as needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using the &lt;em&gt;UserDTO&lt;/em&gt; Class
&lt;/h2&gt;

&lt;p&gt;Now that we have created the &lt;em&gt;UserDTO&lt;/em&gt; class, we can use it to transfer data between different layers of our application. Here is an example of how to use the &lt;em&gt;UserDTO&lt;/em&gt; class to create a new instance of the DTO and convert it to an array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    $userDTO = UserDTO::create([
        'id' =&amp;gt; 1,
        'name' =&amp;gt; 'John Doe',
        'email' =&amp;gt; 'john.doe@example.com',
    ]);

    $userArray = $userDTO-&amp;gt;toArray();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;DTOs are a powerful tool for improving the separation of concerns in your code and making your application more robust and maintainable. By encapsulating your data and using a DTO to transfer it between different layers, you can ensure that your application is not tightly coupled to specific data structures, making it easier to change and evolve over time.&lt;/p&gt;

&lt;p&gt;Moreover, by using traits like &lt;em&gt;ToArray&lt;/em&gt; and &lt;em&gt;StaticCreateSelf&lt;/em&gt;, we have shown how to write clean and reusable DTOs that are easy to use and maintain. These traits can be a great starting point for writing your own DTOs, allowing you to leverage the power of PHP's object-oriented features and create code that is easy to read, understand, and modify.&lt;/p&gt;

&lt;p&gt;So why not give it a try? Start using DTOs in your next project and see how they can help you write better, more maintainable code. Your future self (and your team) will thank you for it!&lt;/p&gt;

</description>
      <category>php</category>
      <category>cleancode</category>
      <category>learning</category>
      <category>development</category>
    </item>
    <item>
      <title>Becoming a software developer: Collaboration, Exploration, and Creating Value</title>
      <dc:creator>shadowtje1990</dc:creator>
      <pubDate>Thu, 06 Apr 2023 16:56:02 +0000</pubDate>
      <link>https://forem.com/shadowtje1990/becoming-a-software-developer-collaboration-exploration-and-creating-value-4p8j</link>
      <guid>https://forem.com/shadowtje1990/becoming-a-software-developer-collaboration-exploration-and-creating-value-4p8j</guid>
      <description>&lt;p&gt;As developers, we all know the feeling of being stuck. It's that moment when you're faced with a problem, and no matter how hard you try, you just can't seem to make any progress. It's frustrating, it's demotivating, and it can leave you feeling like you'll never get any better.&lt;/p&gt;

&lt;p&gt;But the truth is, every developer has been there at some point in their career. It's a natural part of the learning process, and it's something that we all have to work through. The good news is that there are ways to break through that barrier and continue to grow as a developer.&lt;/p&gt;

&lt;p&gt;For many developers, the key to making progress is &lt;em&gt;collaboration&lt;/em&gt;. It's about working with other developers, sharing knowledge and experience, learning from each other's successes and failures and can develop a deeper understanding of the tools and technologies that you work with every day. This is particularly true for developers who are just starting out in their careers. &lt;/p&gt;

&lt;p&gt;But what is next? Collaboration isn't the only key to making progress as a developer. It's also important to keep &lt;em&gt;exploring&lt;/em&gt;. Try out new libraries and frameworks. Start your own little projects and keep it light and fun. This can be a great way to learn new things and to stay engaged and motivated.&lt;/p&gt;

&lt;p&gt;When you want to keep growing to become a better  software developer. You will realise that your focus shifts to &lt;em&gt;producing scalable and sustainable artifacts of value&lt;/em&gt;. Master the different technical skills like knowledge of programming languages, software architectures, testing methodologie, and more is important but the real thing is asking yourself &lt;em&gt;how do I bring value.&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;First of all, what is &lt;em&gt;value&lt;/em&gt;? &lt;br&gt;
For me, &lt;strong&gt;value is anything that makes you feel good or gives you the opportunity to do more good.&lt;/strong&gt; For instance, when you automate something, it gives you time back to do more good.&lt;/p&gt;

&lt;p&gt;The most simplistic form of value is that we don't like pain. You create value when you take someone from pain to benefit. &lt;br&gt;
Another way is 'benefit' to 'benefit' . You can improve or create awareness of what the benefit does. For example, your phone might tell you "You spent 2 hours less on your phone compared to last week" or "Thanks, calendar, for reminding me it's my parents' anniversary".&lt;/p&gt;

&lt;p&gt;Lastly, there's negative value. An example of negative value is when "The service you provide stopped working", "We made some UX improvements, but when you click on more details of a product, you instantly go to the checkout", or "Changing product names for the 10th time this month". Negative value is quite difficult to deal with. It's important to stay on top of the value that you deliver. Analyze the successes or failures, make no assumptions, and test as humanly and reasonably possible within the time frame that you have.&lt;/p&gt;

&lt;p&gt;In the next article, I will dive deeper into the topic of producing scalable and sustainable artifacts of value. I will discuss some strategies for creating software that can grow and evolve over time, while also providing value to users and customers. So stay tuned, and get ready to take your software development skills to the next level!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>productivity</category>
      <category>career</category>
    </item>
    <item>
      <title>Creating Immutable Objects in PHP: A Look at Private Constructor and Public Readonly</title>
      <dc:creator>shadowtje1990</dc:creator>
      <pubDate>Sun, 02 Apr 2023 23:40:54 +0000</pubDate>
      <link>https://forem.com/shadowtje1990/creating-immutable-objects-in-php-a-look-at-private-constructor-and-public-readonly-32eg</link>
      <guid>https://forem.com/shadowtje1990/creating-immutable-objects-in-php-a-look-at-private-constructor-and-public-readonly-32eg</guid>
      <description>&lt;p&gt;When it comes to object-oriented programming in PHP, two commonly used concepts for controlling object creation and initialization are private constructors and public readonly properties. PHP private constructor and public readonly are two different concepts that serve different purposes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Private constructor&lt;/strong&gt;&lt;br&gt;
A private constructor is a constructor method that is declared as private, which means it can only be called from within the class itself. This is often used in the context of the Factory design pattern, where a class is responsible for creating instances of itself or other classes. By making the constructor private, the class ensures that instances can only be created through its own methods, which gives it more control over how instances are created and initialized.&lt;/p&gt;

&lt;p&gt;Overall, private constructors can help reduce language ambiguity by providing a consistent and controlled way for objects to be created and initialized. This can help make code easier to maintain and update, and can improve the overall reliability and security of code that uses the language.&lt;/p&gt;

&lt;p&gt;An example of this concept is this &lt;em&gt;User&lt;/em&gt; class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User {
  private string $email;
  private string $name;

  private function __construct(string $email, string $name) {
    $this-&amp;gt;email = $email;
    $this-&amp;gt;name = $name;
  }

  public static function create(string $email, string $name): User {
    return new self($email, $name);
  }

  public function email(): string {
    return $this-&amp;gt;email;
  }

  public function name(): string {
    return $this-&amp;gt;name;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, the constructor is declared as private so that it cannot be called from outside the class. Instead, we provide a public static method &lt;em&gt;create()&lt;/em&gt; that creates a new instance of the &lt;em&gt;User&lt;/em&gt; class using the private constructor.&lt;/p&gt;

&lt;p&gt;To create a new &lt;em&gt;User&lt;/em&gt; object, you would call the &lt;em&gt;create()&lt;/em&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$user = User::create('johndoe@example.com', 'John Doe');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can then access the email and name properties using the email() and name() methods, respectively:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo $user-&amp;gt;email(); // Outputs: johndoe@example.com
echo $user-&amp;gt;name(); // Outputs: John Doe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Readonly&lt;/strong&gt;&lt;br&gt;
On the other hand, a public readonly property is a property that can be read from outside the class, but cannot be modified. This is achieved by declaring the property as public, but also using the readonly modifier in PHP 8.0 or from PHP 8.2 onwards declare the class as readonly. This ensures that the property’s value can only be set once, usually during object instantiation, and then remains unchanged throughout the object’s lifetime.&lt;/p&gt;

&lt;p&gt;Following this concept our &lt;em&gt;User&lt;/em&gt; class will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User {
  public readonly string $email;
  public readonly string $name;

  public function __construct(string $email, string $name) {
    $this-&amp;gt;email = $email;
    $this-&amp;gt;name = $name;
  }

  public function email(): string {
    return $this-&amp;gt;email;
  }

  public function name(): string {
    return $this-&amp;gt;name;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the &lt;em&gt;$email&lt;/em&gt; and &lt;em&gt;$name&lt;/em&gt; properties are declared as public readonly, which means they can only be set once during object construction using the class constructor.&lt;/p&gt;

&lt;p&gt;To create a new &lt;em&gt;User&lt;/em&gt; object, you would call the constructor and pass in the email and name values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$user = new User('johndoe@example.com', 'John Doe');
You can then access the email and name properties using the email() and name() methods, respectively:

echo $user-&amp;gt;email(); // Outputs: johndoe@example.com
echo $user-&amp;gt;name(); // Outputs: John Doe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you try to modify the email or name property after object construction, you will get a runtime error. For example, the following code will throw an error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$user = new User('johndoe@example.com', 'John Doe');
$user-&amp;gt;email = 'newemail@example.com'; // Throws a runtime error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How truly immutable is a class defined as readonly or with readonly properties?&lt;/strong&gt;&lt;br&gt;
A class with public readonly properties can be considered immutable only if the properties themselves are truly immutable. In other words, if the values of the properties cannot be changed after they are set, then the class can be considered immutable. However, if the properties can be changed after they are set, then the class cannot be considered truly immutable.&lt;/p&gt;

&lt;p&gt;It’s important to note that in PHP, there is no native support for truly immutable objects or properties. Even if a property is declared as public readonly, it is still possible for the value of that property to be changed from outside the class using reflection or other means.&lt;/p&gt;

&lt;p&gt;Furthermore, even if a property is immutable, the class as a whole may not be immutable if it has methods that modify the internal state of the object. In other words, immutability is not just about the properties themselves, but also about the behavior of the class and whether it allows its internal state to be changed after instantiation.&lt;/p&gt;

&lt;p&gt;Therefore, while a class with public readonly properties can provide some level of immutability, it is not a foolproof way to ensure immutability. To truly create immutable objects, it may be necessary to use a combination of techniques such as private properties with only getter methods, final classes that help prevent the modification of object state after instantiation.&lt;/p&gt;

</description>
      <category>php</category>
      <category>architecture</category>
      <category>cleancode</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
