PHP Class Visibility क्या है? | Public, Private, Protected को आसान हिंदी में समझें

PHP class visibility (दृश्यता) एक महत्वपूर्ण concept है जो object-oriented programming (OOP) का मूलभूत हिस्सा है। यह निर्धारित करता है कि class के properties और methods को कहाँ से access किया जा सकता है। इस ब्लॉग पोस्ट में हम PHP class visibility के तीनों प्रमुख access modifiers – public, protected और private को हिंदी में विस्तार से समझेंगे।

PHP Class Visibility क्या होती है?

PHP class visibility यह define करती है कि class के members (properties और methods) को कहाँ से access किया जा सकता है। यह code encapsulation का एक महत्वपूर्ण हिस्सा है जो data security और proper code organization सुनिश्चित करता है।

PHP Class Visibility 3 प्रकार की होती है

Public Visibility

class Example {
    public $publicProperty = 'Public Variable';
    
    public function publicMethod() {
        return 'Public Method';
    }
}
PHP
  • Public members को class के अंदर, बाहर और child classes में कहीं से भी access किया जा सकता है।
  • यह सबसे कम restrictive visibility है।
  • Default रूप से, PHP में सभी class members public होते हैं।

Protected Visibility

class Example {
    protected $protectedProperty = 'Protected Variable';
    
    protected function protectedMethod() {
        return 'Protected Function';
    }
}
PHP
  • Protected members को केवल उसी class के अंदर और उसके child (inherited) classes में access किया जा सकता है।
  • Class के बाहर से इन्हें directly access नहीं किया जा सकता।
  • यह inheritance hierarchy में data को secure रखने में मदद करता है।

Private Visibility

class Example {
    private $privateProperty = 'Private Variable';
    
    private function privateMethod() {
        return 'Private Method';
    }
}
PHP
  • Private members को केवल उसी class के अंदर ही access किया जा सकता है जिसमें वे define किए गए हैं।
  • Child classes या class के बाहर से इन्हें access नहीं किया जा सकता।
  • यह सबसे ज्यादा restrictive visibility है।

PHP Visibility Modifiers का Practical उदाहरण

class Vehicle {
    public $model;          // सार्वजनिक
    protected $color;       // संरक्षित
    private $serialNumber;  // निजी
    
    public function __construct($model, $color, $serial) {
        $this->model = $model;
        $this->color = $color;
        $this->serialNumber = $serial;
    }
    
    public function getColor() {
        return $this->color;  // संरक्षित property को class के अंदर access करना
    }
}

class Car extends Vehicle {
    public function showColor() {
        return $this->color;  // संरक्षित property को child class में access करना
    }
    
    public function showSerial() {
         return $this->serialNumber;  // यह error देगा क्योंकि private property को access 
                                      // नहीं कर सकते
    }
}

$vehicle = new Vehicle("SUV", "Red", "ABC123");
echo $vehicle->model;       // सार्वजनिक - कोई error नहीं
// echo $vehicle->color;    // संरक्षित - error
// echo $vehicle->serialNumber; // निजी - error
echo $vehicle->getColor();  // संरक्षित property को public method के through access करना
PHP

PHP Class Visibility के Best Practices

  1. Encapsulation का पालन करें: Properties को default रूप से private या protected रखें और उन्हें access करने के लिए public methods (getters/setters) provide करें।
  2. Least Privilege Principle: Members को केवल उतनी ही visibility दें जितनी उन्हें वास्तव में आवश्यकता हो।
  3. Documentation: Complex visibility rules को code comments में document करें।
  4. Consistency: पूरे project में visibility modifiers का consistent use करें।

Things To Remember

VisibilitySame ClassSub ClassOutside Class
public✔️✔️✔️
protected✔️✔️❌
private✔️❌❌

Conclusion:

PHP class visibility (public, protected, private) OOP के मूलभूत सिद्धांतों में से एक है जो code organization और security में महत्वपूर्ण भूमिका निभाती है। सही visibility modifier का चुनाव करके आप अधिक maintainable, secure और reusable code लिख सकते हैं।

अगर आपको यह गाइड helpful लगी हो तो इसे अपने developer friends के साथ share करें और comments में अपने सवाल पूछें!

error: Content is protected!! You are not allowed to copy.