Question

I see getter and setter code that has been implemented as a implicit de facto standard in our PHP apps, to the effect that every instance variable in the project classes might as well be public.

I don't want to see a list of getters and setters of every single instance variable in code every time I open a class. They're usually the first thing I see and usually the length of them surpasses the core content of the class which does something other than set and get, so I have to spend a minute scrolling like a muppet in a sea of getters and setters, especially when they are arranged like this:

$this->getter();
$this->setter();
$this->actualstuff();
$this->getter();
$this->setter();
$this->getter();
$this->getter();
$this->setter();
$this->actualstuff();
$this->getter();
$this->setter();

When I am on my death bed I will realize that half of my life will have been wasted looking at setters and getters, so I'm trying to appeal to my colleagues to not automatically generate them upon finishing instance variables out of habit and getting them pointlessly depended upon.

The issue here is that my colleagues like that they appear in the IDE (we use PHPstorm) code completion, which makes things easier when typing out.

I'm wondering if there is a technique to get getter code completion that doesn't use magic methods. A phpstorm plugin or PHP class perhaps.

class Product{
    private $price;
    private $amount;

    public function toGetter(){
        $return = array();
        $return['price'] = $this->price;
        $return['amount'] = $this->amount;
        $getter = new GetterObject($return); //Hypothetical solution class
        return $getter;
    }
}

$Product = new Product();
$Product->toGetter()->getPrice(); //appears in PHPstorm code completion

I'll take any other kind of suggestion.

Was it helpful?

Solution

Getters and setters are a necessary evil in the world of OOP. Yes, if a class is big enough it can be a pain to read through a list of getters and setters, but with the help of a well setup enviroment and or IDE, this does not have to get in the way of productivity and readability.

In the worst case, the actual boilerplate of getters and setters is so commonplace that it's easy to skim when reading and makes the code easier to reason about, if not boring.

In the best case, extending the class becomes child's play and remains easier to read. Most code, especially in larger code bases is Write Once, Read Many(, change infrequently) so the added time taken to implement the getters and setters is not something often repeated in a single class, and with the use of a well configured IDE such as Vim, or an IDE such as PHPStorm, this is a non-issue anyway.

OTHER TIPS

My preferred way of working nowadays (at least for domain code) is to leave the setter out completely (immutability) and generate "getters" as fluent-human-readable language expression [1].

Configure PhpStorm

The idea is basically the same as in Can IntelliJ generate getters without the "get" prefix?.

If you want to configure this for PhpStorm (2019.2):

Go to Settings > Editor > File and Code Templates optionally switch Schema: Default to Schema: Project

Than you can replace the Code > PHP Getter Method with the following Velocity Code Snippet:

/**
 * @return ${TYPE_HINT}
 */
public ${STATIC} function $NAME.substring(0,1).toLowerCase()$NAME.substring(1)()#if(${RETURN_TYPE}): ${RETURN_TYPE}#else#end
{
#if (${STATIC} == "static")
    return self::$${FIELD_NAME};
#else
    return $this->${FIELD_NAME};
#end
}

[1] V. Vernon, Implementing Domain-Driven Design, Addison-Wesley Professional, 2013, p. 246.

Based on @tmhpfnr's answer, if you want to keep the is prefix for boolean types, you can use

public ${STATIC} function #if($GET_OR_IS.substring(0,2) == “is”)is${NAME}#{else}$NAME.substring(0,1).toLowerCase()$NAME.substring(1)#end ()#if(${RETURN_TYPE}): ${RETURN_TYPE}#else#end
{
#if (${STATIC} == “static”)
    return self::$${FIELD_NAME};
#else
    return $this->${FIELD_NAME};
#end
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top