Home > PHP, PHP Tutorials, PHP5 OOPS Tutorials > PHP5 Tutorial OOPS – PHP5 Class Access Specifiers – public, private and protected

PHP5 Tutorial OOPS – PHP5 Class Access Specifiers – public, private and protected

November 14th, 2007 admin Leave a comment Go to comments

In the earlier tutorials we have witnessed keywords like public, private and protected. These are nothing but access specifiers. So, lets understand what access specifiers are.

Definition of Access Specifiers
Access specifiers specify the level of access that the outside world (i.e. other class objects, external functions and global level code) have on the class methods and class data members. Access specifiers can either be public, private or protected.

Why do we need Access specifiers
Access specifiers are used as a key component of Encapsulation and Data Hiding. By using either of the access specifiers mentioned above i.e. public, private or protected you can hide or show the internals of your class to the outside world.

Explanation of each access specifier

1. Private
2. Protected
3. Public




1. Private
A private access specifier is used to hide the data member or member function to the outside world. This means that only the class that defines such data member and member functions have access them. Look at the example below:

class Customer {
	private $name;
 
	public function setName($name) {
		$this->name = $name;
	}
 
	public function getName() {
		return $this->name;
	}
}
 
$c = new Customer();
$c->setName("Sunil Bhatia");
echo $c->name; //error, $name cannot be accessed from outside the class
               //$name can only be accessed from within the class
 
echo $c->getName(); //this works, as the methods of the class have access
                    //to the private data members or methods

In the above example, echo $c->name will give you an error as $name in class Customer has been declared private and hence only be accessed by its member functions internally. Therefore, the following line echo $c->getName() will display the name.

2. Public
A public access specifier provides the least protection to the internal data members and member functions. A public access specifier allows the outside world to access/modify the data members directly unlike the private access specifier. Look at the example below:

class Customer {
	public $name;
 
	public function setName($name) {
		$this->name = $name;
	}
 
	public function getName() {
		return $this->name;
	}
}
 
$c = new Customer();
$c->setName("Sunil Bhatia");
echo $c->name;		// this will work as it is public.
$c->name = "New Name" ; // this does not give an error.

In the above example, echo $c->name will work as it has been declared as public and hence can be accessed by class member functions and the rest of the script.

3. Protected
A protected access specifier is mainly used with inheritance. A data member or member function declared as protected will be accessed by its class and its base class but not from the outside world (i.e. rest of the script). We can also say that a protected data member is public for the class that declares it and it’s child class; but is private for the rest of the program (outside world). Look at the example below:

 
class Customer {
	protected $name;
 
	public function setName($name) {
		$this->name = $name;
	}
 
	public function getName() {
		return $this->name;
	}
}
 
class DiscountCustomer extends Customer {
 
	private $discount;
 
	public function setData($name, $discount) {
		$this->name = $name; //this is storing $name to the Customer
				     //class $name variable. This works
				     // as it is a protected variable
 
		$this->discount = $discount;
	}
}
 
$dc = new DiscountCustomer();
$dc->setData("Sunil Bhatia",10);
echo $dc->name; // this does not work as $name is protected and hence
		// only available in Customer and DiscountCustomer class

In the above example, echo $dc->name will not work work $name has been defined as a protected variable and hence it is only available in Customer and DiscountCustomer class.

You will learn more about inheritance later in this tutorial series. You should revisit this tutorial and read more on the protected section again when you understand inheritance better.

Important Note of Access Specifier in PHP5
In PHP5, access specifiers are public by default. This means that if you don’t specify an access specifier for a data member or method then the default ‘public’ is applicable.

Feel free to write comments if you need more examples or if you need to ask a question on PHP5 Class. You can also subscribe to my notification service to be informed as an when a new tutorial article goes online. Subscribe Below

Related Posts on PHP5 Tutorial – Object Oriented Programming (OOPS)

  1. PHP5 Tutorial – Learn to create a PHP5 Class
  2. PHP5 Tutorial – Learn to Create a PHP5 Class Object
  3. PHP5 Tutorial – Defining Attributes of a PHP5 Class
  4. PHP5 Tutorial – Defining Methods of a PHP5 Class
  5. PHP5 Tutorial – Creating a PHP5 Constructor __construct()
  6. PHP5 Tutorial OOPS – Creating a PHP5 Destructor __destruct()
  7. PHP5 Tutorial OOPS – PHP5 Class Access Specifiers – public, private and protected
  8. PHP5 Tutorial – Magic Methods – __toString() method
  9. PHP5 Tutorial – Magic Methods – __get() and __set()
  10. PHP5 Tutorial – Magic Methods – __isset() and __unset()
  11. PHP5 Tutorial – Magic Methods – __call() method
  12. PHP5 Tutorial – Magic Methods – __autoload() method
  13. PHP5 Tutorial – Magic Methods – __sleep() and __wakeup()
  14. PHP5 Tutorial – Magic Methods – __clone() method

Categories: PHP, PHP Tutorials, PHP5 OOPS Tutorials Tags:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41
  1. November 15th, 2007 at 03:03 | #1

    great series of tutorials, i hope you keep them coming,
    thank you very much.

  2. Rajes Bhatia
    January 14th, 2008 at 17:27 | #2

    This tutorial is helpful to me to understand the concept of access specifiers in php5 but please give some example of their use in web programming.

    As I am fresher and new in web development. I am good in oops concepts and want to implement in web development.

    Thanks

    Regards,

    Rajesh Bhatia

  3. Rahul Kumar
    February 23rd, 2008 at 12:57 | #3

    Dear sir,
    your php 5 OOP tutorial is very helpfull for me.
    Now I understand that what is access specifiers and how these access specifiers works.
    1. Private
    2. Protected
    3. Public
    Thanks
    Regards,
    Rahul

  4. Rakesh Bhatt
    March 3rd, 2008 at 15:30 | #4

    Awesome.. Excellent Series.

    The examples and explanation provided in each section seems quite friendly. no one has explained me things clearly as you have done..

    Thanks Buddy :)

  5. Hetal Sagar
    May 8th, 2008 at 11:35 | #5

    Conscies preicious and towards goal.
    little and powerful explation.

    want more from u on each topic of php5.
    thanks.
    hetalsagar(at)rediffmail(dot)com

  6. Ahmed Fahim
    May 11th, 2008 at 19:43 | #6

    I just read your tutorials once and i understood the differences of the PHP OOP access specifiers. Its very good tutorials and well explained. Thank you so much… I look forward to see a small PHP OOP application if possible in the future… God bless you…

  7. Krishna Prasad
    June 17th, 2008 at 23:01 | #7

    This is simplest and the easiest tutorial i have come across which explains in a very basic and understanding language without making it complex. It was really helpful for me.

  8. Gerard Vignes
    September 26th, 2008 at 06:56 | #8

    Thanks Sunil for using simple examples that illustrate the concept without introducing lots of superfluous details. Your style is needed on the PHP documentation, which sometimes uses bizarre examples that do more to confuse than clarify. I especially appreciate your using a simple inheritance example to illustrate the use of protected visibility on class members. Many other sites try to illustrate this concept without inheritance, which doesn’t make much sense.

  9. December 1st, 2008 at 11:32 | #9

    This is simplest and the easiest tutorial i have come across which explains in a very basic and understanding language without making it complex. It was really helpful for me

    your php 5 OOP tutorial is very helpfull for me.
    Now I understand that what is access specifiers and how these access specifiers works.
    1. Private
    2. Protected
    3. Public

    This tutorial is helpful to me to understand the concept of access specifiers in php5 but please give some example of their use in web programming.

    As I am fresher and new in web development. I am good in oops concepts and want to implement in web development.

    I just read your tutorials once and i understood the differences of the PHP OOP access specifiers. Its very good tutorials and well explained. Thank you so much… I look forward to see a small PHP OOP application if possible in the future… God bless you…

    Thanks Sunil for using simple examples that illustrate the concept without introducing lots of superfluous details. Your style is needed on the PHP documentation, which sometimes uses bizarre examples that do more to confuse than clarify. I especially appreciate your using a simple inheritance example to illustrate the use of protected visibility on class members. Many other sites try to illustrate this concept without inheritance, which doesn’t make much sense.

  10. agus
    December 11th, 2008 at 12:03 | #10

    sir, your series of PHP 5 OOPS is very very usefull for me, many big thanks

    Regard
    Agus

  11. agus
    December 13th, 2008 at 04:12 | #11

    Sir, what the difenrence if we declere the acces specifiers or not? e.g just write:
    $name
    or
    var $name
    rather than
    public $name

    Thank You

  12. rakibul
    January 1st, 2009 at 01:05 | #12

    great tutorial , keep posting please !!!!!!!!

  13. sombir Jangra
    June 26th, 2009 at 12:01 | #13

    your php 5 OOP tutorial is very helpfull for me.
    Now I understand that what is access specifiers and how these access specifiers works.
    1. Private
    2. Protected
    3. Public

    Sir, what the difenrence if we declere the acces specifiers or not? e.g just write:
    $name
    or
    var $name
    rather than
    public $name

    Thank You

  14. Vidya
    July 8th, 2009 at 15:51 | #14

    Excellent tutorial. superb explaination. can you please give some more examples for tough concepts? Also i am looking forward a Zend framework tutorial from you since you explain the concepts better. Thank you very much Sunil !

  15. prabhu kumar
    July 17th, 2009 at 09:40 | #15

    very good tutorials. try to give the more tutorials.

  16. Harish
    July 18th, 2009 at 16:31 | #16

    Very nicely explained the difference of public,private and protected data types in PHP.

  17. August 9th, 2009 at 16:19 | #17

    Thank you verry much , verry helpfull.
    Like this i want to be teached .

  18. sam
    September 23rd, 2009 at 12:03 | #18

    document are gud which heip me very much but there should be more example for access specifier.

  19. alexander timbal
    September 28th, 2009 at 09:44 | #19

    THANKS FOR THE TUTORIAL ITS NICE.

  20. Rashmi Gandhe
    October 27th, 2009 at 14:20 | #20

    very good tutorials.
    Thank You Very Much.

  21. Kike
    November 14th, 2009 at 06:40 | #21

    This is a great tutorial for OOP in php. I’m a newbie working with php scripts.

    I’ve been studying class inheritance in php and I’d like to see some examples of using global scope resolution to assign values from a base class members to a child class member.

  22. dinesh
    November 28th, 2009 at 21:54 | #22

    very nice tutorial……but i just want to know is it necessary to specify access specifiers for each and every data members in a class…..hoping for a reply………..thanx..

  23. December 7th, 2009 at 20:04 | #23

    // Private Property in parent class IS BROKEN DOWN in subclass ?
    // We have 2 classes :
    class User // Parent class
    {
    private $name; // This is problem. It’s private
    protect $pass; // The second property is protect.
    }
    // Subclass
    class Admin extends User
    {

    public function in_pass()
    {
    echo “Name is “.$this->name.”With pass is ” . $this->pass.”";
    }

    }
    // now we run it :
    $HLP = new Admin();
    $HLP->name = “Linh”; // It should have had error but it still runs normally (because it is private property)
    $HLP->pass = “123456″; // But This one is die (of couse, its property is protect.
    $HLP->in_pass();

    // if we change the property of $pass -> private or public , Program runs very well :O
    // My PHP version is 5.3.0.
    // Please help me to explain it. Thanks.

  24. nphp101[dot]yc
    December 14th, 2009 at 05:18 | #24

    Yes, Indeed, VERY HELPFUL! Thanks for sharing your knowledge to everyone. Keep posted such articles!

  25. Mahesh Lakshe
    December 18th, 2009 at 17:54 | #25

    Great !! Thanks for your helpful article over oops concepts in php .

  26. Vikram
    January 1st, 2010 at 07:02 | #26

    Good tutorials. Nice work!

  27. Inderjit
    January 19th, 2010 at 13:23 | #27

    Your tutorials are realy helpful to me to understand the concept of oops php5 but please give some example of their use in web programming.

    Thanks

    Inderji Singh

  28. Nusrat Ali
    February 12th, 2010 at 01:46 | #28

    Thanks
    Really very helpful tutorial, all of us should share knowledge with other for our coming generation.

    Thank you

  29. February 18th, 2010 at 18:56 | #29

    Excellent work Sir. Really awesome. The content you have published on your website is really helpful for a novice and a PHP developer like me. Thanks and keep posting.

  30. March 11th, 2010 at 12:40 | #30

    it’s very helpful for me…………..from this article
    now classes in php is very clear to me……….

    keep this article………..
    thanks???

    sarfaraj
    B.Tech
    KOLKATA

  31. Anil Gomez.D
    January 26th, 2012 at 05:31 | #31

    it’s very helpful for me…………..from this article
    now classes in php is very clear to me………. Thanks for sharing your knowledge to everyone

    keep this article………..
    thanks???
    Anil Gomez.D

  1. No trackbacks yet.
Enter this code to leave comment (Sorry, but bots get me crazy :) )