Is it really necessary to use mutator and accessor methods (setter and getter) in PHP? And the performance?

11

I have come to realize that most other libraries use the setters and getters methods (hereafter referred to as mutator and accessor in>), to change the ownership of some class.

For example:

class User
{
    protected $name;

    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    public function getName()
    {
        return $this->name;
    }
}

This is a classic example. I have a class that represents the user, and it has a property called name , which represents the user name. To change the value of name , we need to use User::setName , and to get it, User::getName .

On the one hand we can think that it would be simpler to do something like:

$user = new User;

$user->name = 'Wallace';

I agree that this would be the simplest way. But there are cases where I recognize that it is necessary to create a way to validate how this name can be entered or type the variable.

Example:

public function setName($name)

{
    if (is_string($name) && count(explode(' ', $name)) >= 2))
    {
        $this->name = $name;
        return $this;
    }

    throw new InvalidArgumentException('Name is not valid');
}

Looking at this side (and I always look only on that side), I realize that it is useful to use mutators because it can validate the data that will be defined in a given object (such as type validation, or specific validations for an attribute).

Another detail is that if you are #

But some questions that always come to mind is like a friend of mine who said, "PHP is not Java, you do not have to do such things."

So here are some questions about this:

I've also heard some rumors that filling a class of methods can reduce performance .

Another detail is also that I see implementations of mutator / accessor using magical methods __isset , __get and __set , or __call and __callStatic . Some say that there is also a question of performance reduction when we use them.

Thus:

  • When should I use mutator / accessor in a class? Is this form used to standardize the code, or for an implementation, or is it just to imitate Java? (the last comment I actually heard)

  • Why do libraries written in PHP (and "people") like to use mutator / accessor rather than defining a public property for a class and defining it "externally "?

asked by anonymous 29.01.2016 / 19:36

2 answers

8

The main reason for using getter and setter methods is to have something beyond simple access and assignment in the property.

So if you do not have a processing, should not you use it? Is not it. It might be interesting to use yes. And then he answers the last question. It is the old response of abstraction. When you do this, you are guaranteed that you can change the implementation of the access / assignment at any time. You can either take or process, after all the access will always be done by the method. If you allow direct access to the variable, you can not change this without affecting the code that consumes the property.

After all there is no problem in computing that can not be solved with an extra level of indirection .

It's also interesting for the above interface case. This is the reason for having an interface, abstracting the implementation.

So it's not just to imitate Java, it's going to imitate Java code. But you can not use any of this if you prefer to write normal PHP code.

Need to use, no need, it can facilitate future maintenance. If you have a case where it is certain that the implementation can not be changed, you never need to use it.

There is certainly a greater cost in calling a method. But you know, in PHP the difference is small. You have to measure it to see if it's in the way.

    
29.01.2016 / 20:06
8
Getters and Setters were not created in Java but were popularized by it, their mandatory use is part of the hyper abstraction language culture where you can never write something that just works, has to have several levels of abstraction, several methods that do nothing but inflate the amount of lines of code so you can feel like you're programming like a rock star as you furiously type those 10 getters and setters that your IDE generates automatically but you like to do on hand to let passers- gawked at their coding skills.

Zoeiras aside the reason for the use you already know, you create access methods for the property so that if you want to do some processing before you retrieve them (getter) and before storing them (setter), this is definitely a lot useful ... when it makes sense. What happens in practice is you see several classes with dozens of getters / setters that do nothing and never will, after all you may never have to process the attribute at the time of retrieving or setting or still can be done quietly in the constructor, so in the end this is a pattern that most people use without thinking or arguing, not necessarily bad.

As for the performance there is an overhead, after all, you change the direct access of the attribute by calling a method that accesses, even worse in the case of magic methods since you end up dealing with attributes by Reflection . But you're going to have to put a lot of effort into having a palpable negative effect on your application, in the overwhelming majority of cases you should be more concerned with things like caching access to the database rather than looking at micro-optimizations of gettter / setter versus direct attribute access.

In the end I can only say that the use is personal question, you can have a long happy life like dev (dev + happy = runtimeerror) without ever using a getter / setter, but there are always cases where using them would facilitate your life.

    
01.02.2016 / 14:37