0

Today I read a Tutorial about MVC, the guy was using magic-methods (__get & __set()) to access a private member value. (LINK) First I was confused what __get and __set do, but after reading trought the Internet I finally found out, that this methods are called if the member is not accessable from the outside. So far...

But it doesn't make sense to have code like this:

class Foo {
    private $bar;

    public __set($value) {
        $this->bar = $value;
    }

    public __get() {
        return $this->bar;
    }
}

I can use normal getter and setter I guess - Its IMO a way easier to understand:

class Foo {
    private $bar;

    public setBar($value) {
        $this->bar = $value;
    }

    public getBar() {
        return $this->bar;
    }
}

If you want access the member outside with __get & __set you can also make the member public - So I don't get whats the sence of it. Can you told me (maybe with an example) whats the sense of these 2 methods?

Petschko
  • 168
  • 3
  • 16
  • Don't see anything close of what you've written as an example in the link you've provided .. He is using an array, not a single property `$this->vars[$index] = $value;`. therefor the meaning of using `__set` and `__get` to access keys in an index are pretty legit. Or do you want to write for every key an accessor? – dbf Apr 11 '16 at 07:22
  • Thats true but I've seen this on different tutorials too.... And you can also use normal getter and setter to set an array don't you? – Petschko Apr 11 '16 at 07:24
  • As i wrote, it's legitimate to use them, I didn't say it's wise to use them ;) – dbf Apr 11 '16 at 07:26
  • I know that they might be legimate but I don't get the sense at all =) Thanks anyway for the answer^^ – Petschko Apr 11 '16 at 07:26
  • Possible duplicate of [What are the general recommendations regarding using magic for getters and setters?](http://stackoverflow.com/questions/7331740/what-are-the-general-recommendations-regarding-using-magic-for-getters-and-sette) – Farside Apr 11 '16 at 08:21

1 Answers1

1

You develop your program , and you consider that you do not have to control access to your attributes. You therefore put the public instead of private . But during maintenance , you realize that you need to change your class to impose a constraint such that it is forbidden to 0 to an attribute. To make this change , you need to add a setter , and spend your attribute in private. Result: it will take you to impact this change to all of this class customers.

If you had used setters and getteurs from the beginning , maintenance would have been much simpler.

DevLoots
  • 747
  • 1
  • 6
  • 21
  • So they are simply for making later changes easier? - But not to build your programm with them – Petschko Apr 11 '16 at 07:26
  • 1
    Object-oriented programming lets you specify the visibility of a class methods and properties. In the case of properties, it is very advisable to give them a public visiblity because that way it becomes possible to change them without that no monitoring is carried out on values. – DevLoots Apr 11 '16 at 07:28
  • 1
    Protect the access of its properties by Getter and Setter called encapsulation . In object-oriented programming , encapsulation is the idea of ​​protecting the information in an object and propose only methods of manipulation of that object. Thus, the properties associated with the information contained in the object will be provided / validated by the methods of the object and will no longer be the responsibility of the external user . The external user can not directly change the information and risk jeopardizing the behavioral properties of the object. – DevLoots Apr 11 '16 at 07:33
  • Sure, that is on my mind. I ever use getter and setter to access members. But I'm using normal getter and setter not those magic getter and setter. The normal getter and setter make sense and I know for what they are for. – Petschko Apr 11 '16 at 07:38