Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Practical 13: OOP Concepts in PHP

Lesson 20 of 35 in the free Web Based Programming Lab notes on Siksha Sarovar, written by Rohit Jangra.

Aim

To implement the core OOP concepts of PHP — class, object, constructor, visibility, inheritance, method overriding and encapsulation — using a Person base class and a Student subclass.

Theory

A class is a blueprint that bundles data (properties) and behaviour (methods); an object is a live instance built from it with new. __construct() is a magic method PHP calls automatically the moment an object is created, so initialisation can never be forgotten. Inside a method, $this refers to the current object (resolved at runtime), while self:: refers to the class itself and is used for static members and constants.

Visibility controls who may touch a member: public (anywhere), protected (the class and its children) and private (only the declaring class). Keeping properties non-public and exposing them only through methods is encapsulation — the object guards its own state, so invalid values can be rejected in one place.

Inheritance (class Student extends Person) gives the child every non-private member of the parent, modelling an "is-a" relationship. The child may override a parent method by redefining it under the same name; calls on a Student object then dispatch to the child's version — this runtime choice is polymorphism. parent:: lets an override extend rather than discard the original: parent::__construct() reuses the parent's setup and parent::intro() reuses its text.

Requirements

  • XAMPP/WAMP with Apache and PHP 8.x, or the PHP CLI / an online PHP compiler
  • Code editor (VS Code); browser or terminal

Procedure

  1. Start Apache from the XAMPP Control Panel.
  2. Save the snippet as p13_oop.php in C:\xampp\htdocs\wbplab.
  3. Open http://localhost/wbplab/p13_oop.php, or run php p13_oop.php in a terminal — the script has no form, so both give the same output.
  4. Experiment: add echo $s->name; at the end and re-run — PHP stops with a fatal Cannot access protected property error, encapsulation in action.

Explanation of the Code

  • class Person declares protected $name — hidden from outside code but visible to subclasses, exactly what Student needs.
  • __construct($name) runs automatically at new time; $this->name = $name copies the argument into the object's own property.
  • intro() returns "I am " . $this->name — behaviour packaged together with the data it uses.
  • class Student extends Person inherits both members and adds private $course, which even a future subclass could not read directly.
  • Student's constructor first delegates with parent::__construct($name) so the base class keeps sole responsibility for $name, then sets its own $course.
  • Student overrides intro(): it calls parent::intro() for the base sentence and appends ", enrolled in " . $this->course — extension, not duplication.
  • $s = new Student("Aman", "BCA") builds the object; echo $s->intro() dispatches to the Student version because the object is a Student — late binding.

Expected Output

A single line: I am Aman, enrolled in BCAparent::intro() contributes I am Aman and the override appends , enrolled in BCA. In a browser the same sentence renders as one line of text.

🎯 Viva Questions

  1. What is the difference between a class and an object? A class is the compile-time blueprint; an object is a runtime instance created from it with new, holding its own copy of the properties.
  2. When does construct() run, and does a child inherit it? Automatically at instantiation. A child without its own constructor inherits the parent's; if it defines one, it must call parent::construct() explicitly to reuse the parent's setup.
  3. Compare public, protected and private. Public: accessible everywhere. Protected: the class plus its subclasses. Private: the declaring class only — even children are locked out.
  4. $this vs self::? $this points to the current object and is resolved at runtime; self:: names the current class, used for statics and constants.
  5. What is method overriding? A child redefines a parent method under the same name; objects of the child then use the new version, and parent::method() can still reach the original.
  6. Where does this program show encapsulation? $name and $course are non-public, so outside code can reach them only through intro() — state is read via behaviour, never directly.

CO Mapping

CO1, CO2