Best to use Private methods or Protected methods?

后端 未结 10 2285
[愿得一人]
[愿得一人] 2020-12-03 09:52

In a lot of my PHP projects, I end up with classes that have non-public functions that I don\'t intend to extend.

Is it best to declare these as protected, or privat

相关标签:
10条回答
  • 2020-12-03 10:16

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

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

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

    If you let classes just have one tightly defined purpose than you can composit objects instead of letting them inherit each other. If you use interfaces rather then inheritance you will most likley define small and effective interfaces. Then you will see that inheritance will reduce and therefore the need of protected will reduce to.

    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 this far from a perfect example. But it illustrates the technique and you can use this in many ways. For instance you can combine this with different creational patterns to build cats and dogs, it may not be so wise to have to know if a cat barks or mews.. But anyway.. it differs from having to make a base class and then extending it with a cat and a dog, and therefor have to make the "sound" method protected or overriding the public eat method

    /Peter

    0 讨论(0)
  • 2020-12-03 10:17

    I generally avoid private. My reasoning goes that if you have an inheritance relation between two classes, and there are private members, then it is a very strong indicative that you should factor the private parts out into a separate object.

    0 讨论(0)
  • 2020-12-03 10:18

    If the function you've implemented is class-specific and shouldn't be used outside of the context of that class, then don't allow it to be inherited. For example, if we had an animal hierarchy, and one of the animals had something incredibly unique to them only, say "layEggsInSand()" like a turtle. This may be wholly unique to the turtle (tortoise, whatever!) therefore shouldn't be inherited by any other animal. In this context we'd say it's private. On the other hand if the function is "walk()" then it's not unique, therefore it should be inheritable.

    It seems quite fuzzy at first because most of the time things should be inherited, but there are rarer cases when they shouldn't be inherited as they're unique to that type.

    0 讨论(0)
  • 2020-12-03 10:24

    I think you should only expose what you need to when you need to. This makes doing impact assessments of changes easier. i.e. If a method is private, you know the impact will be minimal if you change it.

    0 讨论(0)
  • 2020-12-03 10:26

    Well, the private keyword was intended with a purpose. If you don't want the variables to clutter up your inherited classes (or you don't want people playing with them in inherited classes) then why do you even consider making them protected?

    Don't let people touch your private parts :)

    0 讨论(0)
  • 2020-12-03 10:28

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

    Don't worry about what may happen in the future.

    0 讨论(0)
提交回复
热议问题