| | |
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 |
# 5.2.10 alexa apache api array beginner binary broken cakephp checkbox class clean clients cms code cron curl database date directory display dissertation dropdown dynamic echo echo$_get[x]changingitintovariable... email encode error fairness file files folder form forms function functions google href htaccess html image images include indentedsubcategory insert ip javascript joomla legislation limit link local login mail memberships menu mlm multiple multipletables mysql mysqlquery newsletters oop open paypal pdf persist php problem provider query radio random recursion remote rss script search server sessions sms sockets source space spam sql syntax system table tutorial update upload url validator variable video web youtube






