Protected, Private and Public classes – The Difference


by Php Ninza on August 23, 2009

PHP5 allows developers to create public , private and protected methods and properties. They can be defined as :-

Protected:- The specified class and subclasses of that class can use the methods of that class

protected class Test
{
 $dead = false;

 function human()
 {
 }

 function set($dead)
 {
  $this->dead = "true";
 }

}


Public:- The specified class can only use the methods of that class no subclasses can modify the contents of that class

public class Test
{
 $dead = false;

 function human()
 {
 }

 function set($dead)
 {
  $this->dead = "true";
 }

}

Private:- The specified class can use the methods of that class, No Subclasses can use any method of that classes !

private class Test
{
 $dead = false;

 function human()
 {
 }

 function set($dead)
 {
  $this->dead = "true";
 }

}

It’s a good idea to mark functions and classes as protected , This can future proof the code because no method can change the value outside from the class thus reducing the number of hacking attempts.

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

{ 1 comment… read it below or Shout @ me! }

phpcollection August 23, 2009 at 3:08 pm

New Blog post:- Difference between Public, Private and protected classes | PHP tutorials and Scripts Collection http://bit.ly/rAaPZ

This comment was originally posted on Twitter

Leave a Comment

Previous post:

Next post: