Entry
Is it bad PHP style to set an object variable directly?
Should I write get and set methods for my objects?
Feb 8th, 2000 05:21
Nathan Wallace, Gregor Welters, Lars Torben Wilson
There are really two ways to deal with internal variables of objects.
By accessing and setting them directly:
$object->something = "something else";
Or by writing set and get methods on the class which are then used to
access the internal variable.
The second way is cleaner from an OOP perspective, but some of the
other precepts which apply to OOP languages don't apply to PHP either,
since PHP is not an OOP language. Accessing through an object method
in PHP will, however, be slightly slower.
It's up to you. By using the object method, you can perform error
checking etc on the incoming value; however, there is nothing to stop
someone from just setting it to something invalid and skipping the
object method anyway.