•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 423,539 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,227 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 569 | Replies: 2
![]() |
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???
An example would be appreciated.
Thanx
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation:
Rep Power: 8
Solved Threads: 240
Take a look at this .
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
*PM asking for help will be ignored*
*PM asking for help will be ignored*
•
•
Join Date: Jan 2008
Posts: 55
Reputation:
Rep Power: 1
Solved Threads: 5
•
•
•
•
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???
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
![]() |
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Programming FAQ - Updated 1/March/2005 (Computer Science and Software Design)
- Object Oriented PHP Question (PHP)
- PHP, ASP, ColdFusion, what's your fav? (IT Technologies and Trends)
- minor problem with vectors and OOP (C++)
- OOp memory leak error (C++)
- OOP char [8] to char (C++)
Other Threads in the PHP Forum
- Previous Thread: uploading files
- Next Thread: please tell me what is wrong with the code



Linear Mode