Code snippets
Code snippets
Copy-ready answers to everyday coding questions, each with a short explanation. Laravel, PHP, JavaScript, SQL, Bash and more.
2 snippets tagged #immutability · Show all
PHP
October 15, 2026
PHP readonly class with constructor promotion and named arguments
A readonly class makes every property immutable, and constructor promotion removes the boilerplate. “Withers” return a new instance instead of changing the old...
<?php
final readonly class Point
{
JavaScript
October 8, 2026
Deep clone objects in JavaScript with structuredClone()
Makes a true deep copy and keeps Date, Set and Map. It avoids the JSON.parse(JSON.stringify()) hack, which turns Dates into strings and drops Sets. Needs Node 1...
const orig = { user: { name: "Ada", tags: ["a"] }, when: new Date(0), ids: new Set([1, 2]) };
const copy = structuredClone(orig);
copy.user.tags.push("b");
console.log(orig.user.tags, copy.user.tags);