PHP OOP Introduction
In this page:
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
// 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();
?>
Login to try C/C++/Java/PHP code in the editor
OOP के Benefits
एक class एक blueprint है जो define करती है कि उसके objects में क्या properties और methods होंगे, जबकि एक object उस blueprint से बनाया गया एक concrete instance है — काफी हद तक एक house blueprint बनाम इससे बना एक actual house जैसा।
उदाहरण: Benefits of OOP
<?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;
?>
Login to try C/C++/Java/PHP code in the editor
Classes बनाम Objects
PHP ने PHP 4 से OOP support किया है और इसे PHP 5 और बाद में काफी expand किया, इसलिए Laravel और Symfony जैसे modern PHP frameworks लगभग पूरी तरह classes, objects, और उनके साथ आने वाले patterns के आसपास बने हैं।
उदाहरण: Classes vs Objects
<?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;
?>
Login to try C/C++/Java/PHP code in the editor
OOP में Properties
OOP reusability और organization को encourage करता है: एक बार आप एक Product class define कर लें, आप जितने चाहें उतने product objects बना सकते हैं, हर एक अपने खुद के data के साथ लेकिन class में एक बार define की गई same behavior share करते हुए।
उदाहरण: Properties in OOP
<?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;
?>
Login to try C/C++/Java/PHP code in the editor
OOP में Methods
core OOP pillars — encapsulation, inheritance, polymorphism, और abstraction — हर एक एक अलग organizational problem solve करता है, और इस course के बाद के sections concrete PHP examples के साथ हर एक को individually build up करते हैं।
उदाहरण: Methods in OOP
<?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();
?>
Login to try C/C++/Java/PHP code in the editor
- एक class (blueprint) को एक object (
newसे बनाया गया instance) से confuse करना। - किसी method के अंदर property इस्तेमाल करते समय
$this->भूल जाना, जो एक unrelated local variable बना देता है। - हर property को public बनाना, जो किसी भी code को internal state बदलने देता है।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: