PHP Class Visibility In Hindi

📔 : PHP 🔗

पिछले topics में आपने देखा होगा कि Properties / Method को define करने से पहले public keyword का use किया गया था। वह Class Properties / Method का एक visibility type ही था।


किसी भी class में किसी भी type के members (Static या Non Static Properties / Methods ) को define करने से पहले उस Properties / Method की visibility define करनी पड़ती है।


Class Visibility का मतलब Property / Method का scope define करना होता है। की वह Property / Method कहाँ - कहाँ Accessible होगा। PHP में Class Visibility 3 Types की होती है। जिन्हे Access Modifiers भी कहते हैं।

  1. public

    किसी Class में public keyword का use किये गए Property / Method को कहीं से भी access कर सकते हैं। Class के अंदर भी , बाहर भी या इसके Child class में भी access कर सकते हैं।

  2. protected

    protected Property / Method या तो class के अंदर accessible होंगे या Inheritance में इसकी child class में accessible होंगे , इसके अलावा कहीं पर भी इन्हे access नहीं कर सकते हैं।

  3. private

    private Property / Method सिर्फ और सिर्फ Class के अंदर ही accessible होंगे। Class के बाहर कहीं भी इन्हे access नहीं कर सकते हैं।

PHP Class Visibility Example

File : php_visibility.php

Copy Fullscreen Close Fullscreen
<?php  
        class Car
	{
		public $brand_name = 'TATA';
		protected $color = 'Red';
		private $price = 500000;
	}
        $obj = new Car();
	echo $obj->brand_name; /*No Error : Output => TATA */
	echo $obj->color; /*PHP Fatal Error : Cannot access protected property */
	echo $obj->price; /*PHP Fatal Error : Cannot access private property */
?>

Example देखकर आप समझ सकते हैं कि Access Modifiers किस तरह से Work करते हैं। सिर्फ public Property को ही class के बाहर access कर सकते हैं। अब एक और Example देखेंगे , जिसमे methods के अंदर इन properties को access करेंगे।

File : php_visibility.php

Copy Fullscreen Close Fullscreen
<?php  
        class Car
	{
		public $brand_name = 'TATA';
		protected $color = 'Red';
		private $price = 500000;
		public function get_brand()
		{
			return $this->brand_name;
		}

		protected function get_color()
		{
			return $this->color;
		}

		private function get_price()
		{
			return $this->price;
		}
	}
        $obj = new Car();
	echo $obj->get_brand(); /*No Error : Output => TATA */
	echo $obj->get_color(); /*PHP Fatal Error : Call to protected method Car::get_color() from context php_visibility.php*/
	echo $obj->get_price(); /*PHP Fatal Error : Call to private method Car::get_price() from context php_visibility.php */
?>

Property की तरह ही same rules , Methods पर भी apply होते हैं। लेकिन हाँ अगर method public है तो उस method के अंदर हम किसी भी तरह की property access करें इससे कोई फर्क नहीं पड़ता है। Overall method के access modifier पर ही depend करता है।


हालाँकि protected के बारे में आप Next Chapter Inheritance में और अच्छे से समझ पायंगे , कि किस तरह से Parent Class की Properties / Methods को उसकी Child Class में access करते हैं।


I Hope अब आप PHP OOP में Class Visibility / Access Modifiers के बारे में clear हो गए होंगे।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers