
जनता:
जब आप एक विधि (कार्य) या एक संपत्ति (चर) घोषित करते हैं public, तो उन विधियों और गुणों को निम्न तक पहुँचा जा सकता है:
- वही वर्ग जिसने इसे घोषित किया था।
- वे वर्ग जो उपरोक्त घोषित वर्ग को विरासत में देते हैं।
- इस वर्ग के बाहर का कोई भी विदेशी तत्व भी उन चीजों तक पहुंच सकता है।
उदाहरण:
<?php
class GrandPa
{
public $name='Mark Henry'; // A public variable
}
class Daddy extends GrandPa // Inherited class
{
function displayGrandPaName()
{
return $this->name; // The public variable will be available to the inherited class
}
}
// Inherited class Daddy wants to know Grandpas Name
$daddy = new Daddy;
echo $daddy->displayGrandPaName(); // Prints 'Mark Henry'
// Public variables can also be accessed outside of the class!
$outsiderWantstoKnowGrandpasName = new GrandPa;
echo $outsiderWantstoKnowGrandpasName->name; // Prints 'Mark Henry'
संरक्षित:
जब आप एक विधि (फ़ंक्शन) या एक संपत्ति (चर) घोषित करते हैं protected, तो उन विधियों और गुणों को एक्सेस किया जा सकता है
- वही वर्ग जिसने इसे घोषित किया था।
- वे वर्ग जो उपरोक्त घोषित वर्ग को विरासत में देते हैं।
बाहरी सदस्य उन चरों पर नहीं पहुँच सकते। "आउटसाइडर्स" इस अर्थ में कि वे घोषित वर्ग के वस्तु उदाहरण नहीं हैं ।
उदाहरण:
<?php
class GrandPa
{
protected $name = 'Mark Henry';
}
class Daddy extends GrandPa
{
function displayGrandPaName()
{
return $this->name;
}
}
$daddy = new Daddy;
echo $daddy->displayGrandPaName(); // Prints 'Mark Henry'
$outsiderWantstoKnowGrandpasName = new GrandPa;
echo $outsiderWantstoKnowGrandpasName->name; // Results in a Fatal Error
सटीक त्रुटि यह होगी:
PHP घातक त्रुटि: संरक्षित संपत्ति GrandPa :: $ नाम का उपयोग नहीं कर सकता
निजी:
जब आप एक विधि (कार्य) या एक संपत्ति (चर) घोषित करते हैं private, तो उन विधियों और गुणों को निम्न तक पहुँचा जा सकता है:
- वही वर्ग जिसने इसे घोषित किया था।
बाहरी सदस्य उन चरों पर नहीं पहुँच सकते। बाहरी लोग इस अर्थ में कि वे घोषित वर्ग के वस्तु उदाहरण नहीं हैं और यहां तक कि वे वर्ग जो घोषित वर्ग को प्राप्त करते हैं।
उदाहरण:
<?php
class GrandPa
{
private $name = 'Mark Henry';
}
class Daddy extends GrandPa
{
function displayGrandPaName()
{
return $this->name;
}
}
$daddy = new Daddy;
echo $daddy->displayGrandPaName(); // Results in a Notice
$outsiderWantstoKnowGrandpasName = new GrandPa;
echo $outsiderWantstoKnowGrandpasName->name; // Results in a Fatal Error
सटीक त्रुटि संदेश होंगे:
सूचना: अपरिभाषित संपत्ति: डैडी :: $ नाम
घातक त्रुटि: निजी संपत्ति का उपयोग नहीं कर सकते ग्रैंडपा :: $ नाम
परावर्तन का उपयोग करते हुए दादाजी वर्ग को अलग करना
यह विषय वास्तव में दायरे से बाहर नहीं है, और मैं इसे केवल यह साबित करने के लिए यहां जोड़ रहा हूं कि प्रतिबिंब वास्तव में शक्तिशाली है। जैसा कि मैंने उपरोक्त तीन उदाहरणों में कहा था, protectedऔर privateसदस्यों (गुणों और विधियों) को कक्षा से बाहर नहीं पहुँचा जा सकता है।
हालाँकि, प्रतिबिंब के साथ आप कक्षा के बाहर भी पहुँच और सदस्यों द्वारा अतिरिक्त-साधारण कर सकते हैं !protectedprivate
खैर, प्रतिबिंब क्या है?
प्रतिबिंब रिवर्स-इंजीनियर कक्षाओं, इंटरफेस, फ़ंक्शन, विधियों और एक्सटेंशन की क्षमता को जोड़ता है। इसके अतिरिक्त, वे कार्यों, कक्षाओं और विधियों के लिए डॉक्स टिप्पणियों को पुनः प्राप्त करने के तरीके प्रदान करते हैं।
प्रस्तावना
हमारे पास एक वर्ग है जिसका नाम है Grandpasऔर कहते हैं कि हमारे पास तीन गुण हैं। आसान समझ के लिए, विचार करें कि नामों के साथ तीन पोप हैं:
- मार्क हेनरी
- जॉन क्लैश
- विल जोन्स
आइए हम उन्हें बनाते हैं ( क्रमशः संशोधक) public, protectedऔर privateक्रमशः। आप अच्छी तरह से जानते हैं कि protectedऔर privateसदस्यों को कक्षा के बाहर पहुँचा नहीं जा सकता है। अब प्रतिबिंब का उपयोग करते हुए कथन का खंडन करते हैं।
कोड
<?php
class GrandPas // The Grandfather's class
{
public $name1 = 'Mark Henry'; // This grandpa is mapped to a public modifier
protected $name2 = 'John Clash'; // This grandpa is mapped to a protected modifier
private $name3 = 'Will Jones'; // This grandpa is mapped to a private modifier
}
# Scenario 1: without reflection
$granpaWithoutReflection = new GrandPas;
# Normal looping to print all the members of this class
echo "#Scenario 1: Without reflection<br>";
echo "Printing members the usual way.. (without reflection)<br>";
foreach($granpaWithoutReflection as $k=>$v)
{
echo "The name of grandpa is $v and he resides in the variable $k<br>";
}
echo "<br>";
#Scenario 2: Using reflection
$granpa = new ReflectionClass('GrandPas'); // Pass the Grandpas class as the input for the Reflection class
$granpaNames=$granpa->getDefaultProperties(); // Gets all the properties of the Grandpas class (Even though it is a protected or private)
echo "#Scenario 2: With reflection<br>";
echo "Printing members the 'reflect' way..<br>";
foreach($granpaNames as $k=>$v)
{
echo "The name of grandpa is $v and he resides in the variable $k<br>";
}
आउटपुट:
#Scenario 1: Without reflection
Printing members the usual way.. (Without reflection)
The name of grandpa is Mark Henry and he resides in the variable name1
#Scenario 2: With reflection
Printing members the 'reflect' way..
The name of grandpa is Mark Henry and he resides in the variable name1
The name of grandpa is John Clash and he resides in the variable name2
The name of grandpa is Will Jones and he resides in the variable name3
आम गलतफहमी:
कृपया नीचे दिए गए उदाहरण के साथ भ्रमित न करें। जैसा कि आप अभी भी देख सकते हैं, privateऔर protectedसदस्यों को प्रतिबिंब का उपयोग किए बिना कक्षा के बाहर नहीं पहुँचा जा सकता है
<?php
class GrandPas // The Grandfather's class
{
public $name1 = 'Mark Henry'; // This grandpa is mapped to a public modifier
protected $name2 = 'John Clash'; // This grandpa is mapped to a protected modifier
private $name3 = 'Will Jones'; // This grandpa is mapped to a private modifier
}
$granpaWithoutReflections = new GrandPas;
print_r($granpaWithoutReflections);
आउटपुट:
GrandPas Object
(
[name1] => Mark Henry
[name2:protected] => John Clash
[name3:GrandPas:private] => Will Jones
)
डिबगिंग कार्य
print_r, var_exportऔर var_dumpकर रहे हैं डिबगर कार्यों । वे मानव-पठनीय रूप में एक चर के बारे में जानकारी प्रस्तुत करते हैं। ये तीन कार्य PHP 5 के साथ वस्तुओं के गुणों protectedऔर privateगुणों को प्रकट करेंगे । स्टेटिक क्लास के सदस्यों को नहीं दिखाया जाएगा।
और अधिक संसाधनों: