← Back to PHP Course | Chapter 8: OOP | Lesson 4 of 14

PHP Access Modifiers

Access modifiers अलग-अलग locks वाले doors जैसे हैं: public सबके लिए खुला है, protected सिर्फ family के लिए, और private सिर्फ object खुद के लिए। वे important चीज़ों को accidental changes से safe रखते हैं।
Syntax
php
class ClassName {
    public $public_property;       // accessible everywhere
    protected $protected_property; // class and subclasses
    private $private_property;     // this class only
}

Access Modifiers का Introduction

public, protected, और private control करते हैं कि कौन सा code किसी property को पढ़ या modify कर सकता है या किसी method को call कर सकता है, एक class को अपने internal implementation details छुपाते हुए एक clean interface expose करने देते हुए।

उदाहरण: Introduction to Access Modifiers

php
<?php
// Define the class `BankAccount`
class BankAccount {
    // Declare the private property `$balance`, set to `100`
    private $balance = 100;
    // Define the function `getBalance` with no parameters
    public function getBalance() {
        // Return `$this->balance` from this function
        return $this->balance;
    }
}
// Create a new `BankAccount` instance, stored in `$account`
$account = new BankAccount();
// Print `$account->getBalance()` to the output
echo $account->getBalance();
?>

Public Modifier

public members कहीं से भी accessible हैं, class के पूरी तरह बाहर से सहित, और वह default visibility है जो PHP इस्तेमाल करता है अगर आप एक modifier छोड़ दें — हालाँकि इसे explicitly बताना better practice माना जाता है।

उदाहरण: The Public Modifier

php
<?php
// Define the class `Car`
class Car {
    // Declare the public property `$color`, set to "red"
    public $color = "red";
}
// Create a new `Car` instance, stored in `$car`
$car = new Car();
// Print `$car->color` to the output
echo $car->color;
?>

Private Modifier

private members सिर्फ उसी exact class के अंदर से accessible हैं जो उन्हें define करती है, किसी subclass से भी नहीं, जो असल में internal state छुपाने का तरीका है जिसे कोई और code (child classes सहित) directly नहीं छूना चाहिए।

उदाहरण: The Private Modifier

php
<?php
// Define the class `Account`
class Account {
    // Declare the private property `$pin`, set to "1234"
    private $pin = "1234";
    // Define the function `checkPin` taking `$input`
    function checkPin($input) {
        // Return `$input === $this->pin` from this function
        return $input === $this->pin;
    }
}
// Create a new `Account` instance, stored in `$account`
$account = new Account();
// Print a detailed dump (with types) of `$account->checkPin("1234")`
var_dump($account->checkPin("1234"));
?>

Protected Modifier

protected members बीच में बैठते हैं: defining class और इसे extend करने वाली किसी भी class से accessible, लेकिन बाहर के code से नहीं, जो उन internal details के लिए सही choice है जिन पर एक subclass को legitimately build करना ज़रूरी हो।

उदाहरण: The Protected Modifier

php
<?php
// Define the class `Animal`
class Animal {
    // Declare the protected property `$sound`, set to "..."
    protected $sound = "...";
}
// Define the class `Dog`, inheriting from `Animal`
class Dog extends Animal {
    // Define the function `makeSound` with no parameters
    function makeSound() {
        // Print `$this->sound` to the output
        echo $this->sound;
    }
}
// Create a new `Dog` instance, stored in `$dog`
$dog = new Dog();
$dog->makeSound();
?>

Getter और Setter Methods

properties को private या protected तक restrict करना और public methods (getters/setters) के through controlled access expose करना एक core encapsulation technique है जो external code को किसी object को एक invalid state में डालने से रोकता है।

उदाहरण: Getter and Setter Methods

php
<?php
// Define the class `Account`
class Account {
    // Declare the private property `$balance`, set to `0`
    private $balance = 0;
    // Define the function `setBalance` taking `$amount`
    function setBalance($amount) {
        // Check whether `$amount >= 0`
        if ($amount >= 0) {
            $this->balance = $amount;
        }
    }
    // Define the function `getBalance` with no parameters
    function getBalance() {
        // Return `$this->balance` from this function
        return $this->balance;
    }
}
// Create a new `Account` instance, stored in `$account`
$account = new Account();
$account->setBalance(500);
// Print `$account->getBalance()` to the output
echo $account->getBalance();
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. class के बाहर से एक private property access करना, जो एक error throw करता है।
  2. किसी parent class में private इस्तेमाल करना और child classes के इसे access करने की उम्मीद करना, जबकि उन्हें protected चाहिए।
  3. किसी property पर एक access modifier छोड़ देना और इसके private होने की उम्मीद करना, जबकि properties को एक modifier चाहिए और methods default रूप से public होते हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.