What is the difference between a class and an object in PHP? PHP में class और object में क्या अंतर है?
A class is a blueprint or template that defines properties and methods. An object is a concrete instance created from that blueprint, with its own actual values in memory.
class Car {
public string $color;
public function drive() { return $this->color . ' car is driving'; }
}
$car1 = new Car();
$car1->color = 'Red';
$car2 = new Car();
$car2->color = 'Blue';| Aspect | Class | Object |
|---|---|---|
| Nature | Blueprint/template, no memory allocated for data | Actual instance with allocated memory |
| Count | One class definition | Many objects can be created from one class |
| Keyword | class | new |
Interview tip: Use the analogy: class is the architectural blueprint of a house, an object is an actual house built from it - many houses (objects), one blueprint (class).
Class एक blueprint या template है जो properties और methods डिफाइन करता है। Object उस blueprint से बना असली instance है, जिसकी अपनी वैल्यूज़ memory में होती हैं।
class Car {
public string $color;
public function drive() { return $this->color . ' car is driving'; }
}
$car1 = new Car();
$car1->color = 'Red';
$car2 = new Car();
$car2->color = 'Blue';| पहलू | Class | Object |
|---|---|---|
| प्रकृति | Blueprint/template, डेटा के लिए memory allocate नहीं होती | असली instance, memory allocated |
| संख्या | एक class definition | एक class से कई objects बन सकते हैं |
| कीवर्ड | class | new |
इंटरव्यू टिप: यह analogy इस्तेमाल करें: class घर का architectural blueprint है, object उससे बना असली घर - कई घर (objects), एक blueprint (class)।
Was this answer clear?