I got this question put to me recently:
Since you say it’s bad practice to access object properties directly from outside the class, is it then good practice to declare all variables as protected or private to force yourself to not?
Many OO techniques are designed for situations where you will have more than one programmer involved, now or later. By declaring variables as protected or private, you are adding security to the code base by forcing the use of getter and setter methods where you can control how objects are used.
The thing is that with PHP, since the code is not compiled a programmer could just pop open the script and make changes. Whereas with compiled languages like Java, the users of your class have no choice.
… Unless they decompile the class!
This entry was posted on Thursday, December 6th, 2007 at 1:08 pm and is filed under Object Oriented PHP. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.