Neji Posted April 20, 2012 Share Posted April 20, 2012 I'm currently learning PHP and I think I have a decent grasp of procedural programming so I'm trying to set it up a little bit and get a hang of OOP. I'm finding it a little tough going but I am making progress, the syntax and coding of it is fine but I don't quite get some theory and the best way to use it yet. It's only my third day in but I have a question relating to access modifiers. It's a simple one, but what is the point? That may sound a bit narky but I keep hearing and reading that it's good programming convention but it's never really explained why. What am I missing? I get how they work but I don't really get why to use them. I've even read that private should only be rarely used, if at all as it makes testing harder and it stops you from being able to extend your classes. I'm not against using them - I want to learn to program to the very best of my ability but I'm eager to learn what benefits it actually has. Is there a technical reason, for example? Quote Link to comment https://forums.phpfreaks.com/topic/261297-oop-access-modifiers/ Share on other sites More sharing options...
trq Posted April 20, 2012 Share Posted April 20, 2012 Classes are made up of data (properties) and methods to access and manipulate this data. Classes should be designed to have a clear publicly accessible interface. Any other properties or methods that are used to manipulate the data within your class but aren't necessarily useful outside of your class should be made private so as to not confuse the public interface. Maybe an example will help. Lets say you have a User class that represents a User of your application. Now, lets just say for some odd reason that all usernames in your application need to be displayed in all caps. You can build this functionality into the class itself, but there would be no need to make this functionality publically accessible because it is really just to manipulate the classes data. class User private $username; public function __construct($id) { // some code to get user info form the database $this->username = $username; } public function getUsername() { return $this->makeCaps($this->username); } private function makeCaps($str) { return strtoupper($str); } } As you can see by this example. It makes no sense to make the makeCaps() method public as it really has nothing to do with a User. It is simply used internally to manipulate User data. Quote Link to comment https://forums.phpfreaks.com/topic/261297-oop-access-modifiers/#findComment-1339023 Share on other sites More sharing options...
Neji Posted April 21, 2012 Author Share Posted April 21, 2012 Thanks for the reply, good explanation and it does all make sense. So it's more of a logic thing than anything else? No technical benefits? Quote Link to comment https://forums.phpfreaks.com/topic/261297-oop-access-modifiers/#findComment-1339373 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.