PHP OOP Static Members In Hindi

📔 : PHP 🔗

पिछले Topic में आपने समझा कि PHP में Class क्या होती है और इसे कैसे define करते हैं। किसी भी Class में दो तरह के members (Variables / Methods ) हो सकते हैं , Static या Non Static . इस Topic में आप पढ़ेंगे कि Class में Static Members क्या होता है , कैसे इन्हे define करते हैं और किस - किस तरह से इन्हे Access कर सकते हैं।

PHP OOP Defining Static Property

PHP में Static Property / Variables को define करने के लिए simply predefined keyword static का use करके define करते हैं। किसी भी Property / Variables से पहले static लिख देने से वह Property As a static Property define हो जाती है।
For Example :

<?php  
class ClassName
{
public static $property1;
public static $property2;
}
?>

PHP OOP Defining Static Methods

इसी तरह Class में Static Method को define करने के लिए simply Method के name से पहले static keyword का use किया जाता है ।
For Example :

<?php  
class ClassName
{
public static function myfun()
{
// rest of code
}
}
?>

PHP OOP Accessing Static Properties / Methods

PHP में Static Property / Methods Access करने के लिए हमें उस class का Object नहीं बनाना पड़ता है , simply ClassName और Double Colon (::) का use करके हम directly Class की Static Property / Methods Access कर सकते हैं।

PHP OOP Accessing Static Properties / Methods Example

File : php_access_static_members.php

CopyFullscreenClose Fullscreen
<?php   
  class MyCar
  {
     public static $color = 'Green';
     public static function getCar()
     {
       echo "<br> This is MyCar";
     }
 }
 
 /*first try to access property only*/
 echo 'MyCar color is : '.MyCar::$color;
 /*now call the static method*/
 MyCar::getCar();
?>
Output
MyCar color is : Green
This is MyCar

तो कुछ इस तरह से PHP में Static Properties / Methods को directly ClassName के साथ बिना उस Class का Object बनाये Access कर सकते हैं।


हालाँकि ये जरूरी नहीं है , कि Class के अंदर जो value define है वही रहेगी आप उसे अपनी need के according manipulate भी कर सकते हैं।
For Example:

echo 'MyCar color is : '.MyCar::$color;
// Output : MyCar color is Green
MyCar::$color = 'Red';
echo 'Now MyCar color is : '.MyCar::$color;
// Output : Now MyCar color is Red

PHP self

अभी तो Class की Static Properties / Members को Class के बाहर access किया था , लेकिन Class के अंदर उन्हें कैसे access करें ?.

इसके लिए PHP में हम use करते हैं predefined keyword self .

  1.  self का use करके किसी static या non static method में किसी static property / variable को access कर सकते हैं।
  2. किसी static या non static method में static method को access कर सकते हैं।

? सिर्फ Static Properties / Methods को ही self keyword के द्वारा access किया जा सकता है , non static Properties / Methods access करने के लिए $this-> का use किया जाता है हालाँकि इसके बारे में आप Next Topic में पढ़ेंगे।


PHP OOP self Example

File : php_self.php

CopyFullscreenClose Fullscreen
<?php  
       class MyCar
       {
          public static $color = 'Green';
          public static function getCar()
         {
            return self::$color;
         }
         /* define one more static function in which we'll access getCar() function*/
         public static function getCarInfo()
        {
            return self::getCar();
          }
      }
     echo MyCar::getCarInfo();
?>
Output
Green

तो कुछ इस तरह से Class के अंदर self keyword का use करके Static Properties / Methods को access करते हैं।


Important
Normal Function की तरह ही Class में defined Methods में भी हम सभी तरह के Parameters define कर सकते हैं या default parameters set कर सकते हैं।

Related Topics :

Rahul Kumar

Rahul Kumar

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

b2eprogrammers