PHP code example of serafim / immutable

1. Go to this page and download the library: Download serafim/immutable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

serafim / immutable example snippets


class Example
{
    private int $value = 42;

    public function update(int $newValue): self
    {
        $this->value = $newValue;
    
        return $this;
    }
}

class Example
{
    private int $value = 42;

    // Sample #1 (PHP 7.4+)
    public function with(int $newValue): self
    {
        return immutable(fn () => $this->value = $newValue);
    }

    // Sample #2 (PHP 7.3 and below)
    public function with(int $newValue): self
    {
        return immutable(function (): void { $this->value = $newValue; });
    }

}