开发者

What are the general recommendations regarding using magic for getters and setters? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If y开发者_开发知识库ou feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

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).


There are different opinions out there, here is something to read:

  • The great (PHP) getter/setter debate
  • Doing it wrong: getters and setters
  • OOP Getters() And Setters() - A New Programmer's Frustration
  • Is it really that wrong not using setters and getters?
  • Object Overloading in PHP 5


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.


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....


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜