Understanding Classes, Objects, and OOP Principles in PHP
This article introduces the core concepts of object‑oriented programming—classes, objects, abstraction, encapsulation, inheritance, and polymorphism—using clear explanations and practical PHP code examples that demonstrate class definitions, object instantiation, method calls, and inheritance hierarchies.
Class Definition and Example
In PHP, a class defines a template for objects, specifying properties and methods. The following example defines a simple Dog class with $name and $breed properties and bark() and fetch() methods.
<code><?php<br/>class Dog {<br/> public $name;<br/> public $breed;<br/><br/> public function bark() {<br/> echo "Woof! Woof!";<br/> }<br/><br/> public function fetch() {<br/> echo "{$this->name} is fetching.";<br/> }<br/>}<br/>?></code>Object Characteristics
Objects are concrete instances of a class. The example creates two Dog objects, assigns distinct names and breeds, and calls their methods to demonstrate independent state.
<code><?php<br/>// Creating objects from the Dog class<br/>$dog1 = new Dog();<br/>$dog2 = new Dog();<br/><br/>// Setting property values<br/>$dog1->name = "Buddy";<br/>$dog1->breed = "Labrador";<br/>$dog2->name = "Max";<br/>$dog2->breed = "Golden Retriever";<br/><br/>// Calling methods<br/>$dog1->bark(); // Outputs: Woof! Woof!<br/>$dog2->fetch(); // Outputs: Max is fetching.<br/>?></code>Abstraction
Abstraction hides implementation details behind a common interface. An abstract Shape class declares an abstract calculateArea() method, while concrete Circle and Rectangle subclasses implement the method with specific formulas.
<code><?php<br/>abstract class Shape {<br/> abstract public function calculateArea();<br/>}<br/><br/>class Circle extends Shape {<br/> private $radius;<br/> public function __construct($radius) {<br/> $this->radius = $radius;<br/> }<br/> public function calculateArea() {<br/> return pi() * pow($this->radius, 2);<br/> }<br/>}<br/><br/>class Rectangle extends Shape {<br/> private $length;<br/> private $width;<br/> public function __construct($length, $width) {<br/> $this->length = $length;<br/> $this->width = $width;<br/> }<br/> public function calculateArea() {<br/> return $this->length * $this->width;<br/> }<br/>}<br/><br/>$circle = new Circle(5);<br/>$rectangle = new Rectangle(4, 6);<br/>echo 'Circle Area: ' . $circle->calculateArea() . PHP_EOL;<br/>echo 'Rectangle Area: ' . $rectangle->calculateArea() . PHP_EOL;<br/>?></code>Encapsulation
Encapsulation bundles data and behavior, exposing only selected operations. The Car class keeps $model , $color , and $fuelLevel private, providing public getters and a drive() method that safely modifies fuel level.
<code><?php<br/>class Car {<br/> private $model;<br/> private $color;<br/> private $fuelLevel;<br/><br/> public function __construct($model, $color) {<br/> $this->model = $model;<br/> $this->color = $color;<br/> $this->fuelLevel = 100;<br/> }<br/><br/> public function getModel() { return $this->model; }<br/> public function getColor() { return $this->color; }<br/> public function getFuelLevel() { return $this->fuelLevel; }<br/> public function drive() {<br/> $this->fuelLevel -= 10;<br/> if ($this->fuelLevel < 0) { $this->fuelLevel = 0; }<br/> }<br/>}<br/><br/>$myCar = new Car("Toyota", "Blue");<br/>echo "Model: " . $myCar->getModel() . "<br>";<br/>echo "Color: " . $myCar->getColor() . "<br>";<br/>echo "Fuel Level: " . $myCar->getFuelLevel() . "%<br>";<br/>$myCar->drive();<br/>echo "Updated Fuel Level after driving: " . $myCar->getFuelLevel() . "%";<br/>?></code>Inheritance
Inheritance allows a subclass to reuse and extend a parent class. An Animal base class defines a $name property and an eat() method; Dog and Cat subclasses inherit these members and add their own bark() and meow() methods.
<code><?php<br/>class Animal {<br/> public $name;<br/> public function __construct($name) { $this->name = $name; }<br/> public function eat() { return $this->name . ' is eating.'; }<br/>}<br/><br/>class Dog extends Animal {<br/> public function bark() { return $this->name . ' is barking.'; }<br/>}<br/><br/>class Cat extends Animal {<br/> public function meow() { return $this->name . ' is meowing.'; }<br/>}<br/><br/>$dog = new Dog('Buddy');<br/>$cat = new Cat('Whiskers');<br/>echo $dog->eat();<br/>echo $dog->bark();<br/>echo $cat->eat();<br/>echo $cat->meow();<br/>?></code>php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.