User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Dec 2004
Location: India
Posts: 234
Reputation: cancer10 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
cancer10's Avatar
cancer10 cancer10 is offline Offline
Posting Whiz in Training

Question PHP OOP Question

  #1  
Feb 4th, 2008
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: PHP OOP Question

  #2  
Feb 4th, 2008
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*
Reply With Quote  
Join Date: Jan 2008
Posts: 55
Reputation: Walkere is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 5
Walkere Walkere is offline Offline
Junior Poster in Training

Re: PHP OOP Question

  #3  
Feb 4th, 2008
Originally Posted by cancer10 View Post
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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 5:38 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC