0

Hi,


I am new to PHP oop. I was wondering when and why should we use the following:

1) Public
2) Protected
3) Private
4) are there any other???


An example would be appreciated.

Thanx

3
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by Walkere
0

I am new to PHP oop. I was wondering when and why should we use the following:

1) Public
2) Protected
3) Private
4) are there any other???

The link that Naveen posted has a good overview of these OOP concepts.

No, there are no other types - just these three.

As for when to use them... you should always try to use the most restrictive definition for a variable/function. This is an OOP "best practice," and helps ensure your code is easier to maintain in the long run.

As a general rule of thumb, variables should be private or protected - depending on whether inherited classes will be able to access them directly. There are very few variables that should be public - instead, you should write public functions to fetch and change them.

Methods should be public if (and only if) they need to be called from the main script. If they're called from other methods of the class, then they should be private or protected.

- Walkere

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.