Jump to content

OOP - Access Modifiers


Neji

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.