为什么我应该在php中使用__get()和__set()-magic方法? [英] Why I should use __get() and __set()-magic methods in php?

查看:70
本文介绍了为什么我应该在php中使用__get()和__set()-magic方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的副本:
何时/应该使用__construct( ),__ get(),__ set()和__call()在PHP中?

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

这是我尚未找到任何好的答案的一个领域.谁能向我解释为什么/如何正确使用它们?他们不是一个错误的设计决定吗?

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?

推荐答案

我认为

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

将数据写入不可访问的成员时,将运行

__set().

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

__get()用于从无法访问的成员中读取数据.

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

它也有一些使用它们的例子.

It also has some examples of using these.

这篇关于为什么我应该在php中使用__get()和__set()-magic方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆