Is it best to use private methods or protected methods?

In many of my PHP projects, I end up with those who have non-public functions that I do not intend to distribute.

Is it better to declare them protected or private?

I can see the arguments in both directions - making them private - is a much more conservative approach, but it can be argued that they could be protected later if I want this method to be extended and it makes it clear which methods are extended to the base classes .

On the other hand, does it use a private somehow anti-social one, since it prevents the theoretical future developer from expanding my code unchanged?

+33
oop php
Jan 07 '09 at 10:35
source share
10 answers

My instinct is to keep them secret until you need to be otherwise.

It was suggested (unfortunately, I missed the link) that private methods are antisocial, in much the same way as to make them "final", in that he is quite a dictator about how people can use your code.

I am not sure, however, and agree that you should expose only what you really need. An exception would be a library or toolkit where you expect users to want to extend (in a general sense) your code in ways you never anticipated. In this case, the protective methods that were chosen well matched can be considered as providing flexible points.

+24
Jan 07 '09 at 10:51
source share
— -

I think you should only expose what you need, when you need it. This facilitates the assessment of the impact of change. those. if the method is private, you know the impact will be minimal if you change it.

+11
Jan 07 '09 at 10:39
source share

If you intend to build a class for inheritance, you should design it that way, that is, make these methods secure so that other developers can change the behavior of the class according to your lines. Simply creating all protected methods is not a good design. Keep in mind that all protected methods become part of the public API, so if you change things later, you will break other people's code.

In general, if you are not developing for inheritance, you should ban it.

+5
Jan 07 '09 at 10:53
source share

Personally, I consider it necessary to do as much as possible. I just look at each method and ask myself if I want the derived class to call it. Providing all protected leaves opens the door for misuse of methods.

I think it comes down to the questions: "Is everything forbidden, unless it’s allowed" or "Everything is allowed, unless it’s specifically forbidden."

Another factor is that it is easy to make a private method secure in a future version. It is almost impossible to privatize a method when it is protected, since you never know which other code you consider invalid.

+5
Jan 07 '09 at 11:20
source share

Well, a personal keyword was intended for a specific purpose. If you don't want variables cluttering up your inherited classes (or you don't want people playing with them in inherited classes), then why do you even think about protecting them?

Do not let people touch your parts :)

+3
Jan 07 '09 at 11:19
source share

I usually avoid private . My reasoning is that if you have an inheritance relationship between two classes and there are private members, then this is a very strong indication that you should consider the private parts in a separate object.

+3
Jan 09 '09 at 21:44
source share

I would call methods private. It clearly indicates that they are not part of the public API.

Do not worry about what may happen in the future.

+1
Jan 07 '09 at 10:42
source share

If the function you have implemented belongs to a class and should not be used outside the context of this class, do not let it be inherited. For example, if we had a hierarchy of animals, and one of the animals had something incredibly unique to them, say, "layEggsInSand ()", like a turtle. This can be completely unique to a turtle (a turtle, whatever!) Therefore, it should not be inherited by any other animal. In this context, we would say that it is personal. On the other hand, if the function is "walk ()", then it is not unique, so it must be inherited.

It seems rather fuzzy at first, because in most cases things should be inherited, but there are more rare cases when they should not be inherited, since they are unique to this type.

+1
Jan 07 '09 at 10:49
source share

As said, if you are going to extend the class later, you can always change it.

But if you can avoid using inheritance, you should. It is better to use other templates when designing, if possible.

Basically, what to do is support composition over inheritance and program interfaces, not implementations.

If you allow classes to have one clearly defined goal, you can compose objects, rather than letting them inherit from each other. If you use interfaces rather than inheritance, you are likely to define small and efficient interfaces. Then you will see that inheritance will decrease, and therefore the need for protection will decrease.

Example

 interface sound { public function makeSound(); } class bark implements sound{ public function makeSound() { return "Bark!!!"; } } class mew implements sound{ public function makeSound() { return "Mewmew!!!"; } } class forLeggedAnimal { private $sound; public function forLeggedAnimal($sound){ $this->sound = $sound; } public function eat(){ echo $this->sound->makeSound(); } } $cat = new forLeggedAnimal(new mew()); $dog = new forLeggedAnimal(new bark()); 

I know that this is far from a perfect example. But this illustrates the technique, and you can use it in many ways. For example, you can combine this with various creation patterns to create cats and dogs, it may not be that important to know that the cat is barking or ending. But in any case .. it differs from the need to make a base class, and then expand it with a cat and a dog, and therefore they should make the “sound” method a protected or overriding catering method.

/ Peter

+1
Jul 26 '09 at 11:08
source share

I usually use private in a very specific case where the method / variable is very internal and should not be read or written by any other subclass (this almost never happens). The only case when I use a private variable is an example:

 final public function init():void { if(!_initialized) { _init(); _initialized = true; } } protected function _init():void { // specific code. } 

In most other cases, the methods and variable should be useful for inheritance, at least as a read function. Since most of the time when I use code made by someone else and when this code uses personal things, it is always with a lot of problems:

  • desired behavior not yet implemented or completed
  • I cannot use the method because it is closed
  • I cannot change this method because it is closed
  • if it is protected (or if I modify it as protected), then I cannot process the code (also adding behaviors between towing other commands, because part of this code uses private methods or variables ...

    = at the end, you almost change the entire accessor to a protected one in order to be able to expand the class!

Thus, if you think this class can be extended, it is preferable to protect everything except in a very specific case (if you think that a particular method should be used, but not changed, use final protected).

If you think this class should not be extended in any way: use private.

0
May 02 '14 at
source share



All Articles