How you can reduce usage of getter methods in your code
Tags: oop, php
Published: 2018-09-24 07:35:50

---
title: How you can reduce usage of getter methods in your code
published: true
description: Make interaction with your objects explicit
tags: oop, php
---
This is a subject that has been written A LOT, so i'm not going to repeat all those arguments in this post. You can read about them in [here](https://www.javaworld.com/article/2073723/core-java/why-getter-and-setter-methods-are-evil.html) and [here](https://dev.to/scottshipp/avoid-getters-and-setters-whenever-possible-c8m). Instead i'm going to give some examples how you can at least reduce usage of getter methods in your code.

In my view, having to use getting methods in objects is a symptom caused by extensive usage of setting methods. (You can see my thoughts on setters [here](https://dev.to/aleksikauppila/discarding-setters-4oe4).) When we encounter an object whose state may not be valid we have to ask questions to determine if we can work with the object. We depend on object properties and that makes our daily work a struggle.

**Sorting a list**

Sorting a list of employees would maybe need a `getId()` method that you could make sure you only have unique employees. Solution: write an `equals()` method. This allows you to encapsulate the problem of making a unique distinction of two instances of a class.

**Ordering a list**

Maybe you need to order a list of employees by some criteria. Perhaps by name? So you'd need a `getLastName()` and `getFirstName()` methods. Maybe not. If theres a default way to order a list of objects then write a `compareTo()` method. This will put the logic in one place where it naturally belongs.

**Determine if an object is a part of some other object**

Do you need to know if an employee belongs to a company? Write a `belongsTo()` method for the employee. This will allow you to hide the company in the employee object.

**Represent an object in a view**

Do you need to print employee names in a view? Simple, write a `__toString()` method. Now you don't have to expose `$firstName` or `$lastName` to the public. At least for this case...

```php
class Employee
{
    private $id;
    private $firstName;
    private $lastName;

    /** @var Company */
    private $company;
    //...
    public function equals(Employee $employee): bool
    {
        return $this->id === $employee->id;
    }

    public function compareTo(Employee $employee): int
    {
        if (($this->lastName <=> $employee->lastName) === 0) {
            return $this->firstName <=> $employee->firstName;
        }
        return $this->lastName <=> $employee->lastName;
    }

    public function belongsTo(Company $company): bool
    {
        return $this->company->equals($company);
    }

    public function __toString(): string
    {
        return sprintf("%s, %s", $this->lastName, $this->firstName);
    }

}
```

Of course there are other situations where it may be tempting to write a public accessor. Yes, sometimes the simplest way to solve an issue is to write a getter. But it's very important to review all other options before writing that get-method. If your client code starts relying on a piece of data that an object provides, you're stuck with it.

None of those methods i presented expose internal properties. One other thing that makes them useful is that they return very explicit values. `equals()` and `belongsTo()` return a boolean value. A sort of "yes" or "no" answer. I find it extremely useful to provide explicit methods that return boolean values to common questions like `$contract->isTerminated()` or `$contract->isCurrentlyValid()` etc. This is much better than the implicit querying of termination dates and statutes. And of course, it allows us to do proper data hiding. 

BTW, PHP allows asking private attributes inside a class of same object. This comes in very handy when we want to keep our public API clean and minimal. If you know how this can be handled in other languages, please write a comment! Also, if you know other use cases where some concept can help reducing getters, please let me know!