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...
I found a use for properties :)
Moderator: General Moderators
The problem is that you don't always have control over code that was already written... Anyway, here's the situation which made me realise there was a good use for properties...julian_lp wrote:wouldn't be enough just never call a member field directly?I mean, one never should call
$val = $obj->var_name;
Code: Select all
public class BusinessParamters {
public const int MyLimitation = 5;
}
Code: Select all
public class BusinessParameters {
public static int MyLimitation {
get {
// calculate some things...
return 10;
}
}
}