I found a use for properties :)
Posted: Sat Dec 30, 2006 3:58 pm
Perhaps you still remember, but a while ago i asked myself (here): Do we really need properties?
At the time i considered properties to be syntactic sugar (and nothing more) for methods we would otherwise call GetX and SetX...
Earlier this week i figured out that properties actually can do something which methods can't do: They give you the power to replace a variable with the result of a method.
Eg: Originally Foo had a field $bar.. And now some business changes have occured and instead of using a simple value $bar you need to calculate it instead. So you would have to update all the places that use $myInstance->bar with $myInstance->GetBar()...
Well, properties give you the chance to actually leave the external code as is.. The only thing that you have to do is remove the $bar field.. and implement __get/__set so that they call GetBar/SetBar...
At the time i considered properties to be syntactic sugar (and nothing more) for methods we would otherwise call GetX and SetX...
Earlier this week i figured out that properties actually can do something which methods can't do: They give you the power to replace a variable with the result of a method.
Eg: Originally Foo had a field $bar.. And now some business changes have occured and instead of using a simple value $bar you need to calculate it instead. So you would have to update all the places that use $myInstance->bar with $myInstance->GetBar()...
Well, properties give you the chance to actually leave the external code as is.. The only thing that you have to do is remove the $bar field.. and implement __get/__set so that they call GetBar/SetBar...