PHP Classes और Objects
In this page:
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
// 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!";
}
}
?>
Login to try C/C++/Java/PHP code in the editor
Objects Instantiate करना
आप new keyword से किसी class से एक object बनाते हैं, जैसे $myCar = new Car();, जो class की properties की अपनी independent copy के साथ एक fresh instance allocate करता है।
उदाहरण: Instantiating Objects
<?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);
?>
Login to try C/C++/Java/PHP code in the editor
Properties Access और Modify करना
Object properties -> (arrow) operator से access होती हैं, इसलिए $myCar->color = red; उस specific object का color set करता है बिना आपके बनाए किसी दूसरे Car object को affect किए।
उदाहरण: Accessing and Modifying Properties
<?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;
?>
Login to try C/C++/Java/PHP code in the editor
Class Methods लिखना
आप same class से कई independent objects बना सकते हैं, और एक object की properties में changes दूसरे object की properties को कभी affect नहीं करते, क्योंकि हर एक memory में अपनी अलग state रखता है।
उदाहरण: Writing Class Methods
<?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;
?>
Login to try C/C++/Java/PHP code in the editor
$this Keyword इस्तेमाल करना
किसी class के अंदर define किए गए methods उस object की अपनी properties को नाम से सीधे पढ़ और modify कर सकते हैं, उन्हें parameters की तरह pass करने की ज़रूरत के बिना, क्योंकि वे उस specific object के context में execute होते हैं।
उदाहरण: Using the $this Keyword
<?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();
?>
Login to try C/C++/Java/PHP code in the editor
- एक object बनाते समय
newभूल जाना, जैसे$p = Person();, जो एक undefined function call करता है। - किसी static member को access करने के लिए
->या किसी instance member को access करने के लिए::इस्तेमाल करना। - किसी method के अंदर
$this->nameके बजाय$nameलिखना।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: