If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
Class में बिना static keyword के define किये गए सभी Properties / Methods Non Static members होते हैं , By default ये Properties / Methods Non Static ही होते हैं।
Static और Non Static में सबसे बड़ा difference यही होता है कि , Static Members (Property / Methods) Access करने के लिए हमें उस class का Object नहीं बनाना पड़ता है , simply ClassName और Double Colon (::) का use करके हम directly Class की Static Property / Methods Access कर सकते हैं।
जबकि Non Static Members को access करने के लिए Class का Object बनाना ही पड़ेगा , बिना Object बनाये Non Static Members (Property / Methods) को access नहीं कर पाएंगे।
File : php_oop_non_static.php
<?php
class MyClass
{
/*defining non static property*/
public $property = 'This is non static proprty';
/*defining non static method*/
function testfun()
{
echo 'This is non static method';
}
}
/*initialize Object so that we can access them*/
$myobj = new MyClass();
echo $myobj->property;
echo '<br>'; /* for line break */
echo $myobj->testfun();
?>
This is non static property
This is non static method
हालाँकि Object initialization के बाद आप Class property को modify / update भी कर सकते हो।
For Example :
$myobj = new MyClass();
$myobj->property = 'New value';
echo $myobj->property;
//Output : New value
पिछले Example में Non Static Property / Method को Class के बाहर Access किया था , लेकिन अगर हमें Class के अंदर किसी Method में access करना हो तो हम $this का use करते हैं। $this Class में current Object को represent करता है।
जैसे Static Property / Method को access करने के लिए self keyword का use करते थे बैसे ही Non Static Property / Method access करने के लिए $this का use करते हैं।
File : php_oop_this.php
<?php
class MyCar
{
public $color = 'Green';
public function getCar()
{
return $this->color;
}
/* define one more function in which we'll access getCar() function*/
public function getCarInfo()
{
return $this->getCar();
}
}
$myObj = new MyCar();
echo $myObj->getCarInfo();
?>
Green
? सिर्फ non static Properties / Methods access को ही $this के through access कर सकते हैं।
❕ Important
Non Static method में आप Static Properties / Methods को access कर सकते हैं , लेकिन Static Methods में Non Static Properties / Methods को access नहीं कर सकते हैं। क्योंकि Non Static Properties / Methods को access करने के लिए , Object का initialization जरूरी होता है जबकि Static Properties / Methods को बिना Object initialization के भी access कर सकते हैं।
और अगर हम ऐसा करते हैं तो PHP Fatal Error Generate करती है।
PHP Fatal error: Uncaught Error: Using $this when not in object context
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook