← Back to PHP Course | Chapter 8: OOP | Lesson 2 of 14

PHP Classes और Objects

एक class एक cookie cutter जैसा है और एक object इसके साथ बनाई गई एक cookie है। आप shape एक बार define करते हैं, और फिर आप जितनी चाहें उतनी cookies stamp कर सकते हैं।
Syntax
php
class ClassName {
    public $property;

    function method_name() { }
}

$object = new ClassName();
$object->property = value;   // set a property
$object->method_name();       // call a method

एक Class बनाना

आप class keyword के बाद एक name और उससे belong करने वाली properties (variables) और methods (functions) वाली एक body से एक class define करते हैं, जैसे class Car { public $color; function drive() {} }।

उदाहरण: Creating a Class

php
<?php
// Define the class `Car`
class Car {
    // Declare the public property `$color`
    public $color;
    // Define the function `drive` with no parameters
    function drive() {
        // Print "Driving!" to the output
        echo "Driving!";
    }
}
?>

Objects Instantiate करना

आप new keyword से किसी class से एक object बनाते हैं, जैसे $myCar = new Car();, जो class की properties की अपनी independent copy के साथ एक fresh instance allocate करता है।

उदाहरण: Instantiating Objects

php
<?php
// Define the class `Car`
class Car {
    // Declare the public property `$color`
    public $color;
}
// Create a new `Car` instance, stored in `$myCar`
$myCar = new Car();
// Print a detailed dump (with types) of `$myCar`
var_dump($myCar);
?>

Properties Access और Modify करना

Object properties -> (arrow) operator से access होती हैं, इसलिए $myCar->color = red; उस specific object का color set करता है बिना आपके बनाए किसी दूसरे Car object को affect किए।

उदाहरण: Accessing and Modifying Properties

php
<?php
// Define the class `Car`
class Car {
    // Declare the public property `$color`
    public $color;
}
// Create a new `Car` instance, stored in `$myCar`
$myCar = new Car();
$myCar->color = 'red';
// Print `$myCar->color` to the output
echo $myCar->color;
?>

Class Methods लिखना

आप same class से कई independent objects बना सकते हैं, और एक object की properties में changes दूसरे object की properties को कभी affect नहीं करते, क्योंकि हर एक memory में अपनी अलग state रखता है।

उदाहरण: Writing Class Methods

php
<?php
// Define the class `Car`
class Car {
    // Declare the public property `$color`
    public $color;
}
// Create a new `Car` instance, stored in `$car1`
$car1 = new Car();
$car1->color = "red";
// Create a new `Car` instance, stored in `$car2`
$car2 = new Car();
$car2->color = "blue";
// Print `$car1->color . " " . $car2->color` to the output
echo $car1->color . " " . $car2->color;
?>

$this Keyword इस्तेमाल करना

किसी class के अंदर define किए गए methods उस object की अपनी properties को नाम से सीधे पढ़ और modify कर सकते हैं, उन्हें parameters की तरह pass करने की ज़रूरत के बिना, क्योंकि वे उस specific object के context में execute होते हैं।

उदाहरण: Using the $this Keyword

php
<?php
// Define the class `Car`
class Car {
    // Declare the public property `$color`, set to "red"
    public $color = "red";
    // Define the function `showColor` with no parameters
    function showColor() {
        // Print `$this->color` to the output
        echo $this->color;
    }
}
// Create a new `Car` instance, stored in `$car`
$car = new Car();
$car->showColor();
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक object बनाते समय new भूल जाना, जैसे $p = Person();, जो एक undefined function call करता है।
  2. किसी static member को access करने के लिए -> या किसी instance member को access करने के लिए :: इस्तेमाल करना।
  3. किसी method के अंदर $this->name के बजाय $name लिखना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.