Lesson 3
Leveraging OOP Principles in PHP for Enhanced Code Structure
Lesson Overview

Hello once again! Today's lesson is centered around leveraging the principles of Object-Oriented Programming (OOP) — Encapsulation, Abstraction, Polymorphism, and Composition — to enhance code readability and structure. Buckle up for an exciting journey ahead!

Connection between OOP and Code Refactoring

OOP principles act as a scaffold for building readable, maintainable, and flexible code — these are the characteristics we seek while refactoring. By creating logical groupings of properties and behaviors in classes, we foster a codebase that's easier to comprehend and modify. Let's put this into perspective as we progress.

Applying Encapsulation for Better Code Organization

Encapsulation involves bundling related properties and methods within a class, thereby creating an organization that mirrors the real world.

Suppose we possess scattered student information within our program.

php
1$studentName = "Alice"; 2$studentAge = 20; 3$studentGrade = 3.9; 4 5function displayStudentInfo() { 6 global $studentName, $studentAge, $studentGrade; 7 echo "Student Name: " . $studentName . PHP_EOL; 8 echo "Student Age: " . $studentAge . PHP_EOL; 9 echo "Student Grade: " . $studentGrade . PHP_EOL; 10} 11 12function updateStudentGrade($newGrade) { 13 global $studentGrade; 14 $studentGrade = $newGrade; 15}

Although functional, the code could cause potential confusion as the related attributes and behaviors aren't logically grouped. On top of that, all these fields are mutable and accessible from the outer scope, introducing a bunch of security concerns. Let's encapsulate!

php
1class Student { 2 private $name; 3 private $age; 4 private $grade; 5 6 public function __construct($name, $age, $grade) { 7 $this->name = $name; 8 $this->age = $age; 9 $this->grade = $grade; 10 } 11 12 public function displayStudentInfo() { 13 echo "Student Name: " . $this->name . PHP_EOL; 14 echo "Student Age: " . $this->age . PHP_EOL; 15 echo "Student Grade: " . $this->grade . PHP_EOL; 16 } 17 18 public function updateStudentGrade($newGrade) { 19 $this->grade = $newGrade; 20 } 21 22 // Properties for encapsulated fields 23 public function getName() { 24 return $this->name; 25 } 26 27 public function setName($name) { 28 $this->name = $name; 29 } 30 31 public function getAge() { 32 return $this->age; 33 } 34 35 public function setAge($age) { 36 $this->age = $age; 37 } 38 39 public function getGrade() { 40 return $this->grade; 41 } 42 43 public function setGrade($grade) { 44 $this->grade = $grade; 45 } 46}

After refactoring, all student-related properties and methods are contained within the Student class, thereby enhancing readability and maintainability.

Utilizing Abstraction to Simplify Code Interaction

Next up is Abstraction. It is about exposing the relevant features and concealing the complexities.

Consider a code snippet calculating a student's grade point average (GPA) through complex operations:

php
1function calculateGpa($grades) { 2 $totalPoints = 0; 3 $gradePoints = [ 4 "A" => 4, "B" => 3, "C" => 2, "D" => 1, "F" => 0 5 ]; 6 foreach ($grades as $grade) { 7 $totalPoints += $gradePoints[$grade]; 8 } 9 return $totalPoints / count($grades); 10}

We can encapsulate this within the calculateGpa() method of our Student class, thereby simplifying the interaction.

php
1class Student { 2 private $name; 3 private $grades; 4 private $gpa; 5 6 public function __construct($name, $grades) { 7 $this->name = $name; 8 $this->grades = $grades; 9 $this->gpa = $this->calculateGpa(); 10 } 11 12 private function calculateGpa() { 13 $totalPoints = 0; 14 $gradePoints = [ 15 "A" => 4, "B" => 3, "C" => 2, "D" => 1, "F" => 0 16 ]; 17 foreach ($this->grades as $grade) { 18 $totalPoints += $gradePoints[$grade]; 19 } 20 return $totalPoints / count($this->grades); 21 } 22 23 public function getName() { 24 return $this->name; 25 } 26 27 public function setName($name) { 28 $this->name = $name; 29 } 30 31 public function getGrades() { 32 return $this->grades; 33 } 34 35 public function setGrades($grades) { 36 $this->grades = $grades; 37 $this->gpa = $this->calculateGpa(); 38 } 39 40 public function getGpa() { 41 return $this->gpa; 42 } 43}

We can now access the gpa as an attribute of the student object, which is calculated behind the scenes.

Polymorphism to Cater for Multiple Behaviors

Polymorphism provides a unified interface for different types of actions, making our code more flexible.

Assume we are developing a simple graphics editor. Here is a code snippet without Polymorphism:

php
1class Rectangle { 2 public function drawRectangle() { 3 echo "Drawing a rectangle." . PHP_EOL; 4 } 5} 6 7class Triangle { 8 public function drawTriangle() { 9 echo "Drawing a triangle." . PHP_EOL; 10 } 11}

We have different method names for each class. We can refactor this to have a singular draw method common to all shapes:

php
1abstract class Shape { 2 abstract public function draw(); 3} 4 5class Rectangle extends Shape { 6 public function draw() { 7 echo "Drawing a rectangle." . PHP_EOL; 8 } 9} 10 11class Triangle extends Shape { 12 public function draw() { 13 echo "Drawing a triangle." . PHP_EOL; 14 } 15}

Now, regardless of the shape of the object, we can use draw() to trigger the appropriate drawing behavior, thus enhancing flexibility.

Building Better Structure with Composition

Our last destination is Composition, which models relationships between objects and classes. Composition allows us to design our systems flexibly and maintainably by constructing complex objects from simpler ones. This principle helps us manage relationships by ensuring that objects are composed of other objects, thus organizing dependencies more neatly and making individual parts easier to update or replace.

Consider a system in our application that deals with rendering various UI elements. Initially, we might have a Window class that includes methods both for displaying the window and managing content like buttons and text fields directly within it.

php
1class Window { 2 private $content; 3 4 public function __construct() { 5 $this->content = "Default content"; 6 } 7 8 public function addTextField($content) { 9 $this->content = $content; 10 } 11 12 public function display() { 13 echo "Window displays: " . $this->content . PHP_EOL; 14 } 15}

This approach tightly couples the window display logic with the content management, making changes and maintenance harder as we add more elements and functionalities. Let's now see how we can update this code with composition.

Refactoring with Composition

To implement Composition, we decouple the responsibilities by creating separate classes for content management (ContentManager) and then integrating these into our Window class. This way, each class focuses on a single responsibility.

php
1class ContentManager { 2 private $content; 3 4 public function __construct() { 5 $this->content = "Default content"; 6 } 7 8 public function updateContent($newContent) { 9 $this->content = $newContent; 10 } 11 12 public function getContent() { 13 return $this->content; 14 } 15} 16 17class Window { 18 private $manager; 19 20 public function __construct() { 21 $this->manager = new ContentManager(); 22 } 23 24 public function display() { 25 echo "Window displays: " . $this->manager->getContent() . PHP_EOL; 26 } 27 28 public function changeContent($newContent) { 29 $this->manager->updateContent($newContent); 30 } 31}

By refactoring with Composition, we've encapsulated the content management within its class. The Window class now "has a" ContentManager, focusing on displaying the window. This separation allows for easier modifications in how content is managed or displayed without altering the other's logic. Composition, in this way, enhances our system's flexibility and maintainability by fostering a cleaner and more modular design.

Summary

Great job! We've learned how to apply OOP principles to refactor code for improved readability, maintainability, and scalability.

Now, get ready for some exciting exercises. Nothing strengthens a concept better than practice! Happy refactoring!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.