lynnt Posted November 10, 2011 Report Posted November 10, 2011 I was experimenting with the access modifiers in Step 17 of Object Oriented PHP for Beginners. I made pinn_number a private property. I used the classes' constructor to set its value. Finally, I created a public function called get_pinn_number(). Outside of the class, I was able to obtain the value of pinn_number using get_pinn_number. I do not understand why this would work. I understand that the value of making properties private is so that their values are available only within the class where they are defined. Therefore, in practice, why would a developer make the value of a private property available through a public function? In practice, would a developer make get_pinn_number() a private function - and not a public one? This documentation is awesome! Thanks so much!
falkencreative Posted November 10, 2011 Report Posted November 10, 2011 Therefore, in practice, why would a developer make the value of a private property available through a public function? Because they need to access that property? I don't see a reason why this would be bad -- it's considered a best practice to keep a class' variables private, so they can't be accessed/changed directly, and instead access or change them via the class methods. As far as I understand it, if you made get_pinn_number() a private function, it would only be accessible to functions within the class, and the developer wouldn't be able to access the pinn_number variable at all from outside the class.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now