PHP Access Modifiers
In this page:
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
// 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();
?>
Login to try C/C++/Java/PHP code in the editor
Public Modifier
public members कहीं से भी accessible हैं, class के पूरी तरह बाहर से सहित, और वह default visibility है जो PHP इस्तेमाल करता है अगर आप एक modifier छोड़ दें — हालाँकि इसे explicitly बताना better practice माना जाता है।
उदाहरण: The Public Modifier
<?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;
?>
Login to try C/C++/Java/PHP code in the editor
Private Modifier
private members सिर्फ उसी exact class के अंदर से accessible हैं जो उन्हें define करती है, किसी subclass से भी नहीं, जो असल में internal state छुपाने का तरीका है जिसे कोई और code (child classes सहित) directly नहीं छूना चाहिए।
उदाहरण: The Private Modifier
<?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"));
?>
Login to try C/C++/Java/PHP code in the editor
Protected Modifier
protected members बीच में बैठते हैं: defining class और इसे extend करने वाली किसी भी class से accessible, लेकिन बाहर के code से नहीं, जो उन internal details के लिए सही choice है जिन पर एक subclass को legitimately build करना ज़रूरी हो।
उदाहरण: The Protected Modifier
<?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();
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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();
?>
Login to try C/C++/Java/PHP code in the editor
- class के बाहर से एक
privateproperty access करना, जो एक error throw करता है। - किसी parent class में
privateइस्तेमाल करना और child classes के इसे access करने की उम्मीद करना, जबकि उन्हेंprotectedचाहिए। - किसी property पर एक access modifier छोड़ देना और इसके private होने की उम्मीद करना, जबकि properties को एक modifier चाहिए और methods default रूप से public होते हैं।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: