Back to Blog
Lesson 24 of the PHP: Modern PHP from the Ground Up course
PHPAugust 11, 20263 min read

Classes and Objects: Mastering OOP Foundations in PHP

Learn to define classes and instantiate objects in PHP. Master the fundamentals of OOP to structure your code for a scalable, professional MVC application.

PHPOOPclassesobjectsbackend development
Close-up of HTML and PHP code on screen showing error message and login form data.

Previously in this course, we explored connecting models to the database to fetch data. Now, we’ll move beyond simple procedural scripts and embrace Object-Oriented Programming (OOP) to organize our application logic into more robust, reusable components.

From Procedural to Object-Oriented

Until now, our code has been largely procedural: a list of instructions executed from top to bottom. As applications grow, this approach becomes difficult to manage because variables and functions remain "loose" in the global scope.

OOP solves this by allowing us to bundle related data (properties) and behavior (methods) into a single unit called a class. Think of a class as a blueprint, and an object as the actual house built from that blueprint.

Defining a Class

A class is a template. It doesn't perform any work on its own; it simply defines what an object is and what it can do. We define a class using the class keyword followed by a name (conventionally written in PascalCase).

PHP
class Task {
    #6A9955">// Properties represent data
    public $title;
    public $isCompleted = false;

    #6A9955">// Methods represent behavior
    public function complete() {
        $this->isCompleted = true;
    }
}

In this example, $title and $isCompleted are properties. They hold the state of our object. The complete() function is a method. Notice the use of $this: this special variable refers to the specific instance of the object being acted upon. It allows a method to access the properties of the object that called it.

Instantiating an Object

Once a class is defined, we can create multiple instances of it using the new keyword. Each instance is an independent object with its own unique data.

PHP
#6A9955">// Creating an object(instantiation)
$myTask = new Task();

#6A9955">// Accessing properties
$myTask->title = "Learn PHP OOP";

#6A9955">// Accessing methods
$myTask->complete();

echo $myTask->title; #6A9955">// Outputs: Learn PHP OOP
echo $myTask->isCompleted; #6A9955">// Outputs: 1(true)

The arrow operator (->) is how we reach inside an object to touch its properties or trigger its methods.

Hands-on Exercise: Building a User Profile

Let's apply this to our running MVC project. Create a file named User.php and define a class that stores basic profile information.

  1. Define the class: Create a User class with properties for $name and $email.
  2. Add a method: Create a method getSummary() that returns a formatted string like "Name: [name], Email: [email]".
  3. Instantiate: Create a new User object, assign values to your properties, and output the result of getSummary() using echo.

Common Pitfalls

  • Forgetting $this: Beginners often try to access properties inside a method without using $this->. Remember, $this is required to tell PHP which specific object instance you are referring to.
  • Mixing procedural and OOP: You don't need to wrap every single line of code in a class immediately. Start by moving your data-heavy structures (like our database models) into classes first.
  • Case Sensitivity: Class names are case-insensitive in PHP, but sticking to strict PascalCase (e.g., UserTask, not usertask) is the industry standard and prevents confusion.

FAQ

Q: Do I have to use public for my properties? A: Currently, yes. We are using public to allow easy access, but in our next lesson, we will explore "visibility modifiers" that let you control who can see or change your data.

Q: Can I have multiple objects of the same class? A: Absolutely. new Task() creates a unique, isolated instance. If you create $taskA and $taskB, changing a property in $taskA will not affect $taskB.

Q: Is OOP faster than procedural code? A: The performance difference is negligible for most web applications. The real benefit is maintainability—the ability to change code in one place without breaking the entire application.

Recap

We’ve learned that a class acts as a blueprint for creating objects. By bundling data into properties and behavior into methods, we create code that is modular and easier to reason about. This shift to OOP is the foundation for building the professional-grade MVC structure we’ve been working toward throughout this course.

Up next: We will refine our class design by introducing Constructors and Properties, allowing us to initialize objects with specific data the moment they are created.

Similar Posts