PHP OOP में Non-Static Members क्या होते हैं? Php OOP Non Static Member Function

PHP में Object-Oriented Programming (OOP) एक शक्तिशाली प्रोग्रामिंग पद्धति है जो कोड को modular, reusable और maintainable बनाती है। OOP की प्रमुख विशेषताओं में से एक है Non-Static Members का उपयोग।

जब भी हम किसी class में properties या methods बनाते हैं, तो वे by default non-static होते हैं। इसका मतलब है कि उन्हें इस्तेमाल करने के लिए हमें पहले उस class का object (instance) बनाना पड़ता है।

इस लेख में हम जानेंगे कि PHP में Non-Static Members क्या होते हैं, इन्हें कैसे declare और use किया जाता है, इनके क्या लाभ हैं और ये static members से कैसे अलग हैं।

Non-Static Members क्या होते हैं?

Non-static members वे class variables (properties) और functions (methods) होते हैं जिन्हें इस्तेमाल करने के लिए आपको class का object बनाना पड़ता है। ये members हर object के लिए अलग-अलग रहते हैं, यानी हर object की अपनी खुद की copy होती है।

class Car {
    public $brand;

    public function setBrand($name) {
        $this->brand = $name;
    }

    public function getBrand() {
        return $this->brand;
    }
}
PHP

यहाँ $brand, setBrand() और getBrand() non-static members हैं। इन्हें use करने के लिए पहले object बनाना पड़ेगा:

$car1 = new Car();
$car1->setBrand("Toyota");

$car2 = new Car();
$car2->setBrand("Honda");

echo $car1->getBrand(); // Output: Toyota
echo $car2->getBrand(); // Output: Honda
PHP

हर object की अपनी अलग property $brand है।

Types Of Non-Static Members?

  • Non-Static Properties
  • Non-Static Methods

Non-Static Properties

Non-static property एक ऐसा variable होता है जो class के अंदर declare किया जाता है और उसे object के माध्यम से access किया जाता है।

class Student {
    public $name;  // Non-static property
    public $rollNo;
    
    public function __construct($name, $rollNo) {
        $this->name = $name;  // $this का उपयोग
        $this->rollNo = $rollNo;
    }
}

// ऑब्जेक्ट बनाना
$student1 = new Student("राहुल", 101);
$student2 = new Student("प्रिया", 102);

echo $student1->name;  // आउटपुट: राहुल
echo $student2->name;  // आउटपुट: प्रिया
PHP
  • हर ऑब्जेक्ट की अपनी अलग प्रॉपर्टी वैल्यू होती है
  • $this कीवर्ड से एक्सेस करते हैं
  • मेमोरी में हर ऑब्जेक्ट के लिए अलग स्पेस अलॉकेट होता है

Non-Static Methods

Non-static method एक ऐसा function होता है जो class के अंदर define होता है और उसे object से call किया जाता है।

class BankAccount {
    private $balance = 0;  // Non-static property
    
    // Non-static method
    public function deposit($amount) {
        $this->balance += $amount;
    }
    
    public function checkBalance() {
        return $this->balance;
    }
}

// उपयोग
$account1 = new BankAccount();
$account1->deposit(5000);  // केवल $account1 का बैलेंस बदलेगा

$account2 = new BankAccount();
$account2->deposit(2000);  // केवल $account2 का बैलेंस बदलेगा

echo $account1->checkBalance();  // 5000
echo $account2->checkBalance();  // 2000
PHP
  • Non-static मेथड्स के अंदर $this का उपयोग कर सकते हैं
  • इन्हें सीधे क्लास नाम से नहीं बुला सकते (पहले ऑब्जेक्ट बनाना जरूरी)

Static और Non-Static में अंतर

FeatureStatic MembersNon-Static Members
Access MethodClass नाम से (ClassName::)Object से ($object->)
Memory AllocationShared (एक copy)हर object के लिए अलग
Object की जरूरतनहींहाँ
Keywordstatic और self::$this->
UseUtility methods, shared valuesObject-specific डेटा और व्यवहार

Common Mistakes On Creating Member Function

बिना ऑब्जेक्ट बनाए एक्सेस करना

User::login();  // ❌ गलत (Non-static method)
$user = new User(); 
$user->login();  // ✅ सही
PHP

Static मेथड में $this का उपयोग

public static function show() {
    echo $this->name;  // ❌ गलत (Static में $this नहीं)
}
PHP

प्रॉपर्टी को गलत तरीके से एक्सेस करना

echo User->name;  // ❌ गलत (-> के बजाय :: लिख दिया)
PHP

नॉन-स्टैटिक मेम्बर्स कब यूज़ करें?

  1. जब हर ऑब्जेक्ट का अपना अलग डेटा हो (जैसे Users, Products)
  2. जब ऑब्जेक्ट स्टेट (state) मेन्टेन करना हो
  3. जब इनहेरिटेंस (inheritance) का उपयोग करना हो

Conclusion:

PHP में Non-Static Members class के भीतर वो properties और methods होते हैं जो object-specific होते हैं। ये हर object के लिए अलग-अलग डेटा स्टोर करते हैं और $this keyword से access किए जाते हैं।

मुख्य बातें:

  • Non-static members को access करने के लिए object बनाना ज़रूरी होता है।
  • हर object के पास non-static properties की अपनी copy होती है।
  • ये OOP की core principles — जैसे encapsulation और abstraction — को implement करने में मदद करते हैं।
  • Real-world applications में इनका व्यापक प्रयोग होता है।
PHP OOP में Non-Static Members क्या होते हैं
error: Content is protected!! You are not allowed to copy.