Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should we consider using private or protected?

Just wondering, when should we actually must use private or protected for some methods in the model?

Sometimes I can't not be bothered to group my methods in private nor protected. I just leave it as it is. But I know it must be a bad practice, otherwise these 2 groupings won't be created in programming.

Thanks.

like image 421
Victor Avatar asked Jan 04 '12 16:01

Victor


1 Answers

  • If you plan to call a method externally, record.method(), then "public"
  • If it will be used only internally, self.method(), then "private"
  • If you plan to use it internally, but also in descendants, self.method() # in subclass, then "protected"
like image 132
clyfe Avatar answered Sep 23 '22 01:09

clyfe