📘 Lesson · Lesson 15
OOP in PHP
OOP in PHP
OOP in PHP
PHP supports object-oriented programming — organizing code into classes (blueprints) and objects (instances), just like Java and Python.
Class and Object
<?php
class Student {
public $name;
public $marks;
function __construct($name, $marks) {
$this->name = $name;
$this->marks = $marks;
}
function show() {
echo "$this->name: $this->marks";
}
}
$s = new Student("Aman", 88);
$s->show();
?>Aman: 88
Inheritance
<?php
class Person {
function greet() { echo "Hi"; }
}
class Teacher extends Person {
function teach() { echo " teaching"; }
}
$t = new Teacher();
$t->greet(); // inherited
$t->teach();
?>
Summary
- Define a class with fields,
__constructand methods; create objects withnew. - Use
$this->for members andextendsfor inheritance.
PHP में OOP
PHP object-oriented programming support करती है — code को classes (blueprints) और objects (instances) में organize करना, Java और Python की तरह।
Class और Object
<?php
class Student {
public $name;
public $marks;
function __construct($name, $marks) {
$this->name = $name;
$this->marks = $marks;
}
function show() {
echo "$this->name: $this->marks";
}
}
$s = new Student("Aman", 88);
$s->show();
?>Aman: 88
Inheritance
<?php
class Person {
function greet() { echo "Hi"; }
}
class Teacher extends Person {
function teach() { echo " teaching"; }
}
$t = new Teacher();
$t->greet(); // inherited
$t->teach();
?>
सारांश
- fields,
__constructऔर methods वाली class बनाएं;newसे objects। - members के लिए
$this->और inheritance के लिएextends।