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
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
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
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.