为什么我应该在php中使用__get()和__set() - 魔术方法? [重复]

时间:2022-09-10 23:19:17

Possible duplicate:
When do/should I use __construct(), __get(), __set(), and __call() in PHP?

可能的重复:什么时候/我应该在PHP中使用__construct(),__ get(),__ set()和__call()?

This is one area that I have not found any good answer. Can anyone explain to me why/how to use them correctly? Aren't they just a bad design decision?

这是一个我没有找到任何好答案的领域。任何人都可以向我解释为什么/如何正确使用它们?这不是一个糟糕的设计决定吗?

1 个解决方案

#1


I think the documentation explains it best. And it's called Overloading.

我认为文档最好地解释了它。它被称为重载。

__set() is run when writing data to inaccessible members.

将数据写入不可访问的成员时运行__set()。

__get() is utilized for reading data from inaccessible members.

__get()用于从不可访问的成员中读取数据。

It also has some examples of using these.

它也有一些使用这些的例子。

#1


I think the documentation explains it best. And it's called Overloading.

我认为文档最好地解释了它。它被称为重载。

__set() is run when writing data to inaccessible members.

将数据写入不可访问的成员时运行__set()。

__get() is utilized for reading data from inaccessible members.

__get()用于从不可访问的成员中读取数据。

It also has some examples of using these.

它也有一些使用这些的例子。