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
- Start Apache from the XAMPP Control Panel.
- Save the snippet as
p13_oop.phpinC:\xampp\htdocs\wbplab. - Open
http://localhost/wbplab/p13_oop.php, or runphp p13_oop.phpin a terminal — the script has no form, so both give the same output. - 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 Persondeclaresprotected $name— hidden from outside code but visible to subclasses, exactly whatStudentneeds.__construct($name)runs automatically atnewtime;$this->name = $namecopies 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 Personinherits both members and addsprivate $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 callsparent::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 BCA — parent::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
- 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. - 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 callparent::construct()explicitly to reuse the parent's setup. - Compare
public,protectedandprivate. Public: accessible everywhere. Protected: the class plus its subclasses. Private: the declaring class only — even children are locked out. $thisvsself::?$thispoints to the current object and is resolved at runtime;self::names the current class, used for statics and constants.- 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. - Where does this program show encapsulation?
$nameand$courseare non-public, so outside code can reach them only throughintro()— state is read via behaviour, never directly.
CO Mapping
CO1, CO2