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

PHP OOP Introduction

Object-oriented programming programs को objects नाम की चीज़ों से बनाता है, robots से भरे एक toy box जैसा जिनमें हर एक अपने facts और moves जानता है। यह data को इसकी capability के साथ group करके बड़े programs को साफ रखता है।
Syntax
php
class ClassName {
    public $property;

    function method_name() {
        // uses $this->property
    }
}

$object = new ClassName();
$object->method_name();

Object-Oriented Programming क्या है?

Object-oriented programming code को objects के आसपास organize करता है जो data (properties) और behavior (methods) को साथ bundle करते हैं, related variables और functions को किसी script में loosely बिखेरने के बजाय।

उदाहरण: What is Object-Oriented Programming?

php
<?php
// Define the class `Car`
class Car {
    // Declare the public property `$color`, set to "red"
    public $color = "red";
    // Define the function `drive` with no parameters
    function drive() {
        // Print "Driving a $this->color car" to the output
        echo "Driving a $this->color car";
    }
}
// Create a new `Car` instance, stored in `$car`
$car = new Car();
$car->drive();
?>

OOP के Benefits

एक class एक blueprint है जो define करती है कि उसके objects में क्या properties और methods होंगे, जबकि एक object उस blueprint से बनाया गया एक concrete instance है — काफी हद तक एक house blueprint बनाम इससे बना एक actual house जैसा।

उदाहरण: Benefits of OOP

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 = "blue";
// Print `$myCar->color` to the output
echo $myCar->color;
?>

Classes बनाम Objects

PHP ने PHP 4 से OOP support किया है और इसे PHP 5 और बाद में काफी expand किया, इसलिए Laravel और Symfony जैसे modern PHP frameworks लगभग पूरी तरह classes, objects, और उनके साथ आने वाले patterns के आसपास बने हैं।

उदाहरण: Classes vs Objects

php
<?php
// Define the class `Product`
class Product {
    // Declare the public property `$name`
    public $name;
}
// Create a new `Product` instance, stored in `$p`
$p = new Product();
$p->name = "Laptop";
// Print `$p->name` to the output
echo $p->name;
?>

OOP में Properties

OOP reusability और organization को encourage करता है: एक बार आप एक Product class define कर लें, आप जितने चाहें उतने product objects बना सकते हैं, हर एक अपने खुद के data के साथ लेकिन class में एक बार define की गई same behavior share करते हुए।

उदाहरण: Properties in OOP

php
<?php
// Define the class `Product`
class Product {
    // Declare the public property `$name`
    public $name;
    // Declare the public property `$price`
    public $price;
}
// Create a new `Product` instance, stored in `$p1`
$p1 = new Product();
$p1->name = "Laptop";
// Create a new `Product` instance, stored in `$p2`
$p2 = new Product();
$p2->name = "Phone";
// Print `$p1->name . " / " . $p2->name` to the output
echo $p1->name . " / " . $p2->name;
?>

OOP में Methods

core OOP pillars — encapsulation, inheritance, polymorphism, और abstraction — हर एक एक अलग organizational problem solve करता है, और इस course के बाद के sections concrete PHP examples के साथ हर एक को individually build up करते हैं।

उदाहरण: Methods in OOP

php
<?php
// Define the class `Product`
class Product {
    // Declare the public property `$name`, set to "Book"
    public $name = "Book";
    // Define the function `describe` with no parameters
    function describe() {
        // Print `"This product is: " . $this->name` to the output
        echo "This product is: " . $this->name;
    }
}
// Create a new `Product` instance, stored in `$p`
$p = new Product();
$p->describe();
?>
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. एक class (blueprint) को एक object (new से बनाया गया instance) से confuse करना।
  2. किसी method के अंदर property इस्तेमाल करते समय $this-> भूल जाना, जो एक unrelated local variable बना देता है।
  3. हर property को public बनाना, जो किसी भी code को internal state बदलने देता है।

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.