| | |
PHP OOP Question
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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: Jan 2008
Posts: 57
Reputation:
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
![]() |
Similar Threads
- Programming FAQ - Updated 1/March/2005 (Computer Science)
- Object Oriented PHP Question (PHP)
- PHP, ASP, ColdFusion, what's your fav? (IT Professionals' Lounge)
- 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: PHP and MYSQL table formatting
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary body broken cakephp checkbox class cms code cron curl database date date/time directory display download dynamic echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla limit link list login loop mail menu mlm mod_rewrite msqli_multi_query multiple mycodeisbad mysql oop parse paypal pdf php problem query radio random recourse recursion regex remote script search seo server sessions sms soap source space sql static structure syntax system table tutorial update upload url validation validator variable video web webdesign wordpress xml youtube






