Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The point of public/private variables and functions?

Tags:

oop

php

This is not a literal coding question, I'm just wondering, what's the point of using public and private functions and variables? Where would I use each of them?

I've always thought of them as being "old" or not needed anymore, but I'm sure there's a good reason they're used in some places.

like image 998
Kavi Siegel Avatar asked Mar 15 '11 19:03

Kavi Siegel


1 Answers

Basically, when you are developing a class that other developers will use :

  • public methods and variables are what the other developers will (need to) use -- what your class does
  • private methods and variables are what you, as the developer of that class, used to make it work -- it's how your class works internally.

Others need to be able to use your class ; but not to know how it does it.


If you want to know more, you'll have to search for Encapsulation.

like image 60
Pascal MARTIN Avatar answered Sep 23 '22 01:09

Pascal MARTIN