Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

untymage's avatar

What is the point of using Accessor?

I saw this tips in twitter :

image

Why not just return the result and using the traditional method call ? (if we only want to use accessor and not with mutator ) ?

public function formattedStartDate () 
{
    return $this->start_date->format(...);
}

What is the problem with using in this way ?

0 likes
4 replies
Sinnbeck's avatar

There isn't a problem with using it that way, and some even prefer it. Accessors just make it easy to have a unified way of getting column data, whether it's a real column or a fake one. It also allows you to always change a column when accessed (casting to a date or similar)

1 like
martinbean's avatar

@untymage I don’t really understand what you’re asking. The screenshot you posted is an accessor only and doesn’t contain a mutator. It’s just a new way of defining accessor.

untymage's avatar

@martinbean I'm saying why not return the result early and not using anything ? look at the second snippet, I was telling why some people using accessor when we could return the result directly

Please or to participate in this conversation.