2

Coming from strongly typed languages, I'm uncomfortable with the magic getter and setter methods of PHP. What is the motivation behind implementing them into the language and what are the general recommendations regarding using them?

Edit:

I understand the motivation behind using getters and setters in general. I'm interested in the motivation behind using magic getter and setter methods (as opposed to setProperty($property) and getProperty() methods).

Emanuil Rusev
  • 34,563
  • 55
  • 137
  • 201
  • Properties exist in C# and probably other static languages too, the only difference is that dispatch is dynamic and string-based and that's more due to the general static/dynamic worldview (in the same vein you could ask for recommendations regarding potentially having values of different types in a variable). –  Sep 07 '11 at 09:50
  • If you are uncomfortable with the language semantics, then also being opposed to magic setters/getters is not understandable. After all they are the only thing that allows to piggyback type enforcement. – mario Sep 07 '11 at 09:55
  • Getters-setters and typing are orthogonal. – Lightness Races in Orbit Sep 07 '11 at 09:59

4 Answers4

2

What is the motivation behind implementing [__get() and __set()]?

PHP’s magic methods, __get() and __set() are also known as “overloading” methods. From the PHP manual

PHP's interpretation of "overloading" is different than most object oriented languages. Overloading traditionally provides the ability to have multiple methods with the same name but different quantities and types of arguments.

Where as:

Overloading in PHP provides means to dynamically "create" properties and methods. These dynamic entities are processed via magic methods one can establish in a class for various action types.

Basically, __get(), __set(), and __call() were introduce to provide overloading capabilities per the PHP definition of overloading.

What are the general recommendations regarding using them?

This is a loaded question because different people may have differing opinions. That being said, they can be used to:

  • make a property read-only
  • validate data being stored/retrieved.
  • change the interface (e.g. $object->method($data) becomes $object->data)

These are only a few examples. You may find Best Practices for __get() and __set() useful.

Community
  • 1
  • 1
Herbert
  • 5,698
  • 2
  • 26
  • 34
0

In php, if at all you are uncomfortable with the magic getters and setters, you can write them in your class as you have done in strongly typed language and you can use it as you have used it there. There is no restriction at all. I think we can automatically generate Getter and setter functions in some framework like symfony. You can write the appropriate implementation of magic getters and setters, if you need more type enforcement or control over the properties.

Shameer
  • 3,036
  • 1
  • 21
  • 27
0

I see PHP magic getters and setters as one of its sickness.
When a programmer needs to deal with strings inside the code, he and his team starts making mistakes... (this is y I think you should avoid as much as you can from assoc arrays... it is not safe)

The IDE
When you type in your editor $a->get than you get a list of getters and you don't need to remember the exact case of the class member. if a programmer's life wouldn't be easy, it wont be easy for the application.

The programming
In most languages, getters and setters are actually a functions where you can add some small logic, it is not so wisely done in PHP.

The overhead
Every PHP magic function has some performance issues.

This is my opinion....

fatnjazzy
  • 6,070
  • 12
  • 57
  • 83