-1

I've learned OOP for PHP in the last couple of days and have created a login-registration system. When should I use private, protected, and public to initialize a parameter? For example: Let's say I'm creating a password parameter:

protected $password;

Which depth should I use, or does using the public depth not pose security risks?

Also, is there any rule of thumb to figure out which depth to use? Excuse the nooby question, I've just started learning OOP

htmlJohn
  • 13
  • 2
  • Same question but with Java https://stackoverflow.com/questions/215497/what-is-the-difference-between-public-protected-package-private-and-private-in. Also related https://stackoverflow.com/questions/8353272/private-vs-protected-visibility-good-practice-concern and https://stackoverflow.com/questions/4361553/what-is-the-difference-between-public-private-and-protected – j08691 Aug 10 '20 at 16:06
  • 1
    This should be an interesting read: [What is the difference between public, private, and protected?](https://stackoverflow.com/questions/4361553/what-is-the-difference-between-public-private-and-protected) – Qirel Aug 10 '20 at 16:22

1 Answers1

1

The downvotes will pour in because this is considered base level OOP knowledge, so if you struggle with this you might want to consider reading into OOP more. I suggest the following:

https://code.tutsplus.com/tutorials/object-oriented-php-for-beginners--net-12762

https://www.tutorialspoint.com/php/php_object_oriented.htm

As for your question:

These parameters are important for inheritance and what you can use inside and outside of a class.

private is the most enclosed setting, whereby you can not access or interact with anything (method or variable) that is marked private. Try creating a class with a dummy function which echoes out something. If you call this function from within the class, lets say in the constructor (using $this->myfunc() notation), it will echo out as desired. But if you initialise the class and try to call the same function using the arrow notation ($myclass = new Myclass(); $myclass->myfunc();) it will not work because you are trying to access a private member from outside.

protected is the same concept but you can access protected members from child classes (look into child classes and inheritance if you're unsure, this is outside the scope of this question)

public however is you may access it outside or inside the class.

As for a rule of thumb, I usually make anything private (unless there is further inheritance) that I know I don't want to or shouldnt have to call from outside. Anything else can be public. For example a lot of code I write will include having to write a base class which does a certain, extended procedure, for example cloning a github repo and updating files with it across our servers. Anything that is the part of this process (the function of cloning, the function of accessing a certain location, etc) are made private because they are part of a workflow which should never have to be called on their own (there are exceptions of course, but this is a matter of practise). But if i have helpers, let's say to see if the github repo is accessible or not, I might as well make that public because a condition of whether or not we want to start the update process might be to first determine if the repo is accessible to me. This is of course not a very good example but I hope it helps.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
peterxz
  • 864
  • 1
  • 6
  • 23
  • Im curious to know why my answer is wrong? Because I decided to educate instead of referring to an unrelated question? – peterxz Aug 10 '20 at 16:18
  • 1
    A downvote doesn't by default mean "the answer is wrong". There's an alignment of SO users who disagree with the practice of answering "poor" questions to begin with. The thread linked to by @Qirel, an answer already on SO, seems to cover just about everything that needs to be said for an OOP beginner on public/protected/private, examples included. Then, take the downvote not as *"answer is wrong"*, but as *"act of answering was wrong"*, in someone's view. It's considered good practice to not duplicate answers, but rather link to existing answers where some exist. – Markus AO Aug 10 '20 at 16:46