Can you give an example of when to use private and when protected?

I'm here until the end can not understand the advantages of using the private access modifier is protected.
After all, one of the principles of SOLID States that in order to change something in the class, no need to change the code - you need to do it through inheritance. Is it possible in this case to use protected?

Can you give an example when to use private functions and have the properties of a class and when to use protected? Can be in any language.
April 19th 20 at 12:29
3 answers
April 19th 20 at 12:31
Solution
Properties are always private, except may be for example a config array with the default settings, to override the heirs of a property, methods that describe the interface are always public protected if you need to pull them out of the heirs is private methods are helpers that do not belong to the interface and just to do something under the hood
April 19th 20 at 12:33
Solution
To change something in the class, no need to change the code - you need to use the pattern decorator.
There is a design principle: "prefer composition over inheritance, and the principle of "design-level abstractions".
Overall, if you can create a class object - it is not necessary to derive better delegate in the new class part of the functionality for that object and add a new functionality, something like that.
Of course, private is needed for cases when anything outside should not leave (except through a getter).
Protected useful if the principle of design-level abstractions when you can figure out the General (and make protected) and separate changing (another principle, by the way).
April 19th 20 at 12:35
Solution
Mommy's defendants, who wrote about the fact that everywhere should be private, clever books I have not read, in addition to dry theory and something, apparently, not developed.

The class almost ALWAYS have to have protected properties. Because they are no different from private, but allow you to use properties in descendant classes, which very often arise. And that would not rewrite the code, it is always better to use protected.

The situation when you really need private-method or property is a very rare phenomenon.
The majority, as I understand it, puts private by default, but your response to me seems to be the more literate and logical in the PLO. - dorthy_Ebert41 commented on April 19th 20 at 12:38
@dorthy_Ebert41, it's not just my opinion, this opinion was expressed by one teacher from a Minsk University who came to our company in 2013, when the team was retrained with the PHP on Java.

Can you give an example when to use private functions and have the properties of a class and when to use protected?


In this example, I used static methods and properties, but not the essence.
Store counters $data is available only through methods of the abstract class-parent and no other way to this property you will not tunnel.
abstract class Car
{
 private static $data = [];

 protected static function setCounter()
{
 if (!isset(self::$data[static::class])) {
 self::$data[static::class] = 0;
}

self::$data[static::class]++;
}

 public static function getCounter()
{
 return self::$data[static::class] ?: 0;
}
}

class Car extends VAZ
{
 public function __construct()
{
parent::setCounter();
}
}

class BMW extends Car
{
 public function __construct()
{
parent::setCounter();
}
}

new VAZ(); new green(); new green(); new green(); new green();
new BMW();

echo "the Class was called VAZ" . (VAZ::getCounter()) . ", BMV was called " . (BMV::getCounter());
- makenzie commented on April 19th 20 at 12:41
Something that is not needed in the heirs, it is not necessary, if you need to write a property getter, once the object is better not to wear
Sample counters at all strange - jessyca_Jast commented on April 19th 20 at 12:44
@jessyca_Jast,
1. You can't know, especially at the stage of development that will need the heir and what not.
2. Pointless hiding properties of the class drags a bunch of getters and complicate code.
3. PRIVATE, in fact, it is a way of protection from being shot in the leg as it will lead to fatal consequences. Do you want to hide something that is vital to hide. - makenzie commented on April 19th 20 at 12:47

Find more questions by tags OOPPHP