📘 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, __construct and methods; create objects with new.
  • Use $this-> for members and extends for 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
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।