943,649 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 2786
  • PHP RSS
Feb 4th, 2008
0

PHP OOP Question

Expand Post »
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
Similar Threads
Reputation Points: 58
Solved Threads: 1
Posting Whiz in Training
cancer10 is offline Offline
234 posts
since Dec 2004
Feb 4th, 2008
0

Re: PHP OOP Question

Take a look at this .
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Feb 4th, 2008
0

Re: PHP OOP Question

Click to Expand / Collapse  Quote originally posted by cancer10 ...
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
Reputation Points: 29
Solved Threads: 5
Junior Poster in Training
Walkere is offline Offline
57 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: uploading files
Next Thread in PHP Forum Timeline: PHP and MYSQL table formatting





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC