Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is useful if you want to apply <a href="http://en.wikipedia.org/wiki/Object-oriented_programming" rel="noreferrer">OOP</a> practices such as <a href="http://en.wikipedia.org/wiki/Information_hiding" rel="noreferrer">Information Hiding</a>(<a href="http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)" rel="noreferrer">Encapsulation</a>).</p> <p>If you declare class members as <code>private</code>, they cannot be accessed from code outside of your class. You have to provide <em>methods</em> to access them. This separates the interface of your class from the actual implementation.<br> Other code that uses your class does not need to know the name of the class member or <em>how</em> you actually store information internally.</p> <p>Example:</p> <p>Consider a class <code>Books</code> the somehow gives me a list of books. I can define a <code>public</code> member that holds an array of books:</p> <pre><code>class Books { public $list; } </code></pre> <p>On the other side, if I define a method <code>getList()</code>, I can change the implementation later, without effecting the code that uses the class:</p> <pre><code>class Books { private $list; public function getList() { // get list from database, from XML, etc. // might use $list internally but does not have to } } </code></pre> <p>Obviously, you don't <em>need</em> modifiers like <code>private</code>, <code>protected</code> or <code>public</code> to implement this behavior, but it leads to better structured code and clearer interfaces. Imagine you have a class that has both a <code>public $list</code> and a method <code>getList()</code>. How do you know which one of them to use?<br> <strong>This applies not only for <em>getting</em> values, but especially for <em>setting</em> values.</strong></p> <p>There are no disadvantages if you use them, only advantages IMHO. The difference between them is the scope of the visibility. <code>public</code> members can be accessed from outside code, <code>protected</code> members can be accessed form inheriting classes and <code>private</code> members only from the class.</p> <p>Methods can also have these modifiers and follow a similar purpose. E.g. if a method is declared as <code>private</code>, it is clear that it is probably some kind of helper method, that is used internally only and is not supposed to be called form the outside.</p> <hr> <p>So in the end it boils down to two things:</p> <ul> <li><strong>Control</strong>: Which parts of your class can be accessed in which way</li> <li><strong>Self-documentation or understanding</strong>: Other people using your class can figure out more easily, which part of your class they are supposed to access and which not.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload