0

I am learning php oop. But I can not understand in which situations use public, private and protected. I know that public is accessable inside the class and outside the class, protected inside the class and inside classes which inherites it, private is accessable only inside the class. But how to know that the property or method must be protected or private ? I know that if write class for connecting database they must be protected or private. But, an example: I am writing registrating class(is the code below true ?):

 private $email;
 private $username;
 private $password;
 private $securitycode;

 private function register {

 //here registrations codes, may be I must use public function ?

 }

Another example: I have news section in the website and want to get news details (id, title, text, author) and write News class (is given code below true ?):

 private $id;
 private $title;
 private $text;
 private $author;

 public function get_one_news($this->id) {

 //here the code for getting the news, may be I must use protected function ?
 }

Another example: I want to get number of users or news: Which I have to use : public, protected or private function ? Another example: Every user(registered or unregistered) can add comment(id, comment): Can I use public $id; public $comment ? or I have to use protected or private ?

Please, I need your advices. Which(public, protected, private properties and functions) to use if I want to add/get news, to register/login user, to add/edit/get data from database tables, to make fileuploading and etc. ? I could not find answers to my question.

Javid Karimov
  • 435
  • 5
  • 22
  • 1
    Possible duplicate of [What is the difference between Public, Private, Protected, and Nothing?](http://stackoverflow.com/questions/614818/what-is-the-difference-between-public-private-protected-and-nothing) – toesslab Nov 27 '15 at 20:33

1 Answers1

0

You can think about it like this. The non-private parts of your class are its interface to the outside world. You can change the private inner workings as much as you want, without thinking about breaking other code in your system. However as soon as you start changing the non private parts you have to take into consideration all the users of your code depending on your current public interface. So I think as a general rule of thumb you should try to make your code as private as possible.By that you can greatly increase the encapsulation of your codebase, allowing you to change the internal implementation details without affecting the code using your class.

So in a first step think about what functionality your new class should offer its users. This should then become its public interface. Then think about whether or not your class should be inherited from and what parts should be allowed to be changed in its subclasses. Everything else should be private as it is the classes internal implementation.

markus
  • 1,631
  • 2
  • 17
  • 31
  • so it would be better that I can use only protected or private ? – Javid Karimov Nov 27 '15 at 20:31
  • @CavidKərimov not necessarily. Think about what other classes can should be able to do with your class. E.g. with your News class what should others do with it? (ask a specific news instance for a textual repesentation, ask for its news category,...). These should then be the public methods of that class. – markus Nov 28 '15 at 07:07