This page will probably be useful. (Note that what you say is incorrect - __set()
takes as a parameter both the name of the variable and the value. __get()
just takes the name of the variable).
__get()
and __set()
are useful in library functions where you want to provide generic access to variables. For example in an ActiveRecord class, you might want people to be able to access database fields as object properties. For example, in Kohana PHP framework you might use:
$user = ORM::factory('user', 1);
$email = $user->email_address;
This is accomplished by using __get()
and __set()
.
Something similar can be accomplished when using __call()
, i.e. you can detect when someone is calling getProperty() and setProperty() and handle accordingly.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…