Practical examples of immutability in PHP

A mutable object can be modified after its creation, an immutable cannot.

Image for post
Image for post

An immutable object will remain in the same state as it was created. Design and implementation will be much easier and consistent. In the case of problems, locate a potential bug is faster due that it won’t have side-effects.

Although, creating immutable objects sometimes require more code, and it doesn’t fit in all scenarios (entities need to be mutable for example).

Mutability examples and their impacts

<?php declare(strict_types=1);final class ProductTransfer
public function __construct(
public string $name,
public float $price,
public array $tags,
public \DateTime $releaseDate,
) {}
public function toString(): string
return sprintf(
'Name: %s\nPrice: %.2f\nTags: {%s}\nRelease date: %s\n',
implode(', ', $this->tags),

Being up-to-date and avoiding conflicts

Image for post
Image for post

Have you ever wondered how you could collaborate with open-source projects but you didn’t know how to start? It couldn’t be easier. Take a look:

  • Fork the repository into your account.
Image for post
Image for post
  • Clone the forked project on your computer.
git clone
  • Add the upstream to sync with the new changes to your project.
(master)$ git remote add upstream If you run `git remote -v` you should see:
origin (fetch)
origin (push)
upstream (fetch)
upstream (push)
  • Get the last changes in your project.
(master)$ git fetch upstream master
(master)$ git merge upstream/master
  • Create a new branch. …

Dummy, Stub, Spy, Mock or Fake

Image for post
Image for post

Test Doubles

A Test Double is an object that can stand-in for a real object in a test, similar to how a stunt double stands in for an actor in a movie.

As I wrote in “The importance of the Tests in our Software”, there are several types of tests. They are also known as Test Doubles instead of “Mocks”.

The five types of Test Doubles are:

Image for post
Image for post
  • Dummy: It is used as a placeholder when an argument needs to be filled in.
  • Stub: It provides fake data to the SUT (System Under Test).
  • Spy: It records information about how the class is being used.
  • Mock: It defines an expectation of how it will be used. It will cause a failure if the expectation isn’t met. …

Image for post
Image for post

TL;DR: No, and let me explain you why.

It causes bugs when refactoring due to the highly coupling

When we use reflection our tests get too fragile, we are allowing our tests to know so much information about the real implementation.
We need to hard-code the method name and we are coupling our test method to the production code.
Furthermore, we need to write a lot about boilerplate to test a simple method.

Q: I need to get at least an 80% of code coverage, how can I get it without Reflection class?
A: You should test ONLY your public methods and depend on the variables we pass, we should reach all the possible paths. …

1.- What is a test?

A test is an empirical assertion which demonstrates the behaviour of an expected functionality from something.

Image for post
Image for post

The tests are classified by what they verify, the most important ones are the following:

Functional tests

  • Unit test
  • Component test
  • Integration test
  • System test
  • Smoke test

Non-functional test

  • Compatibility test
  • Security test
  • Stress test
  • Usability test

A software needs all of them, but the most important concerning developers are unit, integration and functional tests.

2.- Differences between test types

Unit test

Image for post
Image for post

What is a unit test? We could consider, in our context, a public method from one API class. That means, a unit test is a verification between the current input and expected output, it is an isolated logic and decoupled from outside. …


Jesus Valera Reales

Competitive, entrepreneur and autodidact. Hard worker, lover of technology and free software.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store