This Keyword In PHP In Hindi.
हेलो दोस्तों आज के इस ब्लॉग पोस्ट(This Keyword In PHP In Hindi) में मैं आपको PHP के अंदर This keyword के बारे में बताने जा रहा हूँ |
अगर आप PHP में प्रोग्रामिंग करते है या फिर अपने PHP के program को देखा हो तो बहुत से program में आपको यह $This keyword देखने को मिला होगा |This Keyword In PHP In Hindi|
यह keyword class के अंदर उपयोग होता है | आमतौर पर यह मेंबर फंक्शन के साथ होता है जो कि क्लास के non -static member (variable एंड functions) को access करते है वो भी current object के लिए|This Keyword In PHP In Hindi|
$This keyword का उपयोग देखने के लिए आप नीचे दिए गए example को देख सकते है |This Keyword In PHP In Hindi|
Example:
<?php
class Person {
// first name of person
private $name;
// public function to set value for name (setter method)
public function setName($name) {
$this->name = $name;
}
// public function to get value of name (getter method)
public function getName() {
return $this->name;
}
}
// creating class object
$john = new Person();
// calling the public function to set fname
$john->setName("John Wick");
// getting the value of the name variable
echo "My name is " . $john->getName();
?>
Output:
My name is John Wick
ऊपर दिए गए प्रोग्राम में हमने एक प्राइवेट वेरिएबल name को declare किया है और इस variable को एक्सेस करने के लिए हमने दो पब्लिक फंक्शन्स setName और getName बनाये है जिसे इस वेरिएबल की वैल्यू set और access कर सकते है |
जब भी हम class में डिक्लेअर किसी भी वेरिएबल को क्लास के अंदर ही declare मेंबर फंक्शन से call करना चाहते है तो फिर हम इस केस में This keyword का use करते है जो कि करंट object को पॉइंट करता है जो कि variable को होल्ड करता है |
This keyword का उपयोग हम किसी class के एक मेंबर फंक्शन को किसी और मेंबर फंक्शन के अंदर भी कॉल कर सकते है |
NOTE: पर हाँ एक बात यहाँ पर ध्यान देने लायक है कि अगर क्लास के अंदर कोई static variable अथवा member function है तो फिर हम इनको This keyword का उपयोग करके refer नहीं कर सकते है |
तो फिर static members के लिए हम क्या उपयोग कर सकते है ?
static members के लिए हम This का उपयोग न करके self का उपयोग करते है | इस self keyword का उपयोग हम scope resolution ऑपरेटर(::) के साथ करते है | चलिए इस एक example से समझते है |
Example:
<?php
class Person {
// first name of person
public static $name;
// public function to get value of name (getter method)
public static function getName() {
return self::$name; // using self here
}
}
?>
अब अगर आपको अभी भी This और self keyword में confusion हो रहा है तो फिर हम आपको इन दोनों के बीच में कुछ अंतर बता कर आपके इस confusion को भी दूर कर देते है |
SELF vs THIS Keyword In Hindi: This Keyword In PHP In Hindi
SELF Keyword | THIS Keyword |
self keyword के आगे कोई भी symbol use नहीं होता है | This keyword के आगे हम $ symbol का उसे करते है | |
किसी भी क्लास के variables अथवा functions को self keyword की मदद से एक्सेस करने के लिए हम scope resolution :: का उपयोग करते है | | This keyword से किसी वेरिएबल अथवा फंक्शन को एक्सेस करने के लिए हम -> सिंबल का उपयोग करते है | |
self keyword का उपयोग हम static मेंबर्स को रेफेर करने के लिए करते है | | This keyword का उपयोग हम non -static मेंबर्स को रेफेर करने के लिए करते है | |
PHP self keyword जो है वो वो क्लास मेंबर्स को रेफेर करता है न कि किसी पर्टिकुलर ऑब्जेक्ट को | ऐसा इसलिए क्योकि जो static members होते है वो सभी ऑब्जेक्ट्स के द्वारा शेयर किये जाते है | | This keyword जो है वो किसी पर्टिकुलर ऑब्जेक्ट के लिए ही क्लास मेंबर्स जैसे variable और function को refer करता है | |
चलिए अब हम एक कोड example की मदद से इसको समझते है जो की नीचे दिया गया है |
Example:
<?php
class Job {
// opening for position
public $name;
// description for the job;
public $desc;
// company name - as the company name stays the same
public static $company;
// public function to get job name
public function getName() {
return $this->name;
}
// public function to get job description
public function getDesc() {
return $this->desc;
}
// static function to get the company name
public static function getCompany() {
return self::$company;
}
// non-static function to get the company name
public function getCompany_nonStatic() {
return self::getCompany();
}
}
$objJob = new Job();
// setting values to non-static variables
$objJob->name = "Data Scientist";
$objJob->desc = "You must know Data Science";
/*
setting value for static variable.
done using the class name
*/
Job::$company = "Studytonight";
// calling the methods
echo "Job Name: " .$objJob->getName()."<br/>";
echo "Job Description: " .$objJob->getDesc()."<br/>";
echo "Company Name: " .Job::getCompany_nonStatic();
?>
Output:
Job Name: Data Scientist
Job Description: You must know Data Science
Company Name: Studytonight
ऊपर दिए गए example में हम कुछ non -static variable रखते है और एक static variable को रखते है |
जैसे कि हम जानते है कि static मेंबर जो है वो क्लास के साथ ही associate रहता न कि किसी ऑब्जेक्ट के साथ इसलिए इसे हम class name की हेल्प से एक्सेस करते है |
और हाँ एक static method जो है वो एक static variable को अपने अंदर declare कर सकता है | और एक non -static method भी एक static variable को अपने अंदर declare कर सकता है | पर इस variable को हम class name की मदद से ही access करते है |
Conclusion:
तो दोस्तों आज के इस ब्लॉग पोस्ट(This Keyword In PHP In Hindi) में हमने php के अंदर This keyword और self keyword के बारे में जाना और पढ़ा | This keyword का उपयोग हम क्लास में declare non -static members जैसे कि variables और functions को access करने के लिए करते है | और static members को एक्सेस करने के लिए हम self keyword का उपयोग करते है | self keyword के आगे कोई symbol नहीं लगा होता है | जबकि This keyword के आगे $ sign लगा होता है | self keyword का उपयोग हम scope resolution के साथ कर सकते है |
इस ब्लॉग(This Keyword In PHP In Hindi) को लेकर आपके मन में कोई भी प्रश्न है तो आप हमें इस पते [email protected]पर ईमेल लिख सकते है|
आशा करता हूँ, कि आपने इस पोस्ट(This Keyword In PHP In Hindi) को खूब एन्जॉय किया होगा|
आप स्वतंत्रता पूर्वक अपना बहुमूल्य फीडबैक और कमेंट यहाँ पर दे सकते है|
आपका समय शुभ हो|