PHP code example of tiny-blocks / immutable-object

1. Go to this page and download the library: Download tiny-blocks/immutable-object 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/ */

    

tiny-blocks / immutable-object example snippets




namespace Example;

use TinyBlocks\Immutable\Immutable;
use TinyBlocks\Immutable\Immutability;

final class Order implements Immutable
{
    use Immutability;

    public function __construct(public int $id, public Products $products)
    {
    }
}

$order = new Order(id: 1, products: new Products(array: ['item1', 'item2']);

$order->id = 2;    # Throws an exception
unset($order->id); # Throws an exception  

$order = new Order(id: 1, products: new Products(array: ['item1', 'item2']);

$order->items[0] = 'item3'; # Throws an exception
unset($order->items[0]);    # Throws an exception