Skip to Content
Course content

16: Classes and Objects

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I've noticed a recurring pattern when people first hit Object-Oriented Programming (OOP) in PHP. They often treat a Class as if it's just a fancy folder or a namespace used to group related functions together. They think, "Okay, instead of having ten different functions for user management floating around my script, I'll just put them inside a User class."

Classes aren't just buckets for functions

If you're using a class simply to house functions that don't actually interact with any internal data, you're not really doing OOP—you're just organizing your code. Here is what that "wrong" approach looks like:

class UserHelper {
    public function formatName($first, $last) {
        return "$first $last";
    }
}

$helper = new UserHelper();
echo $helper->formatName("Jane", "Doe");

Technically, this works. But it's pointless. You've gone through the effort of instantiating an object just to call a function that doesn't care about the object itself. You might as well have just written a standard function. The power of a class isn't the grouping; it's the state.

The Blueprint vs. The House

Think of a class as a blueprint for a house. The blueprint isn't a house; you can't live in a blueprint. You can't open a window on a piece of paper. But you can use that blueprint to build ten different houses. Each house is an Object (or an "instance").

While every house built from the blueprint has a front door and a roof, one house might be painted blue and another might be painted green. That "color" is what we call a Property. The ability to "open the door" is a Method.

Let's look at a real-world example: a Shopping Cart. We don't want a "helper" to manage a cart; we want the cart to be an object that knows what's inside it.

class ShoppingCart {
    // Properties: These hold the "state" of the object
    public $items = [];
    public $totalPrice = 0;

    // Method: This changes the state of the object
    public function addItem($name, $price) {
        $this->items[] = $name;
        $this->totalPrice += $price;
    }
}

// Here we create two distinct "houses" from the same blueprint
$janesCart = new ShoppingCart();
$bobsCart = new ShoppingCart();

$janesCart->addItem("Mechanical Keyboard", 120);
$bobsCart->addItem("USB-C Cable", 15);

// Jane's cart knows it has $120, Bob's knows it has $15. 
// They are completely independent.

Using $this to talk to yourself

You probably noticed that weird $this keyword in the addItem method. This is where most learners get tripped up. $this is how an object refers to itself.

Inside the ShoppingCart class, the code doesn't know if it's currently acting as $janesCart or $bobsCart. It just knows it is a cart. By using $this->totalPrice, you're telling PHP: "Find the totalPrice property belonging to whichever specific object is currently running this method."

I'll give you a pro tip here: as you get deeper into this, you'll start making these properties private instead of public to prevent people from manually changing the total price without actually adding an item. But for now, focus on the relationship: the Class defines the structure, and the Object holds the actual data.




📋 Practical Task

Build a Digital Library Book Tracker

Your task is to move away from "helper functions" and create a proper Object-Oriented system to track books in a library. Follow these requirements:

  • Create a class named Book.
  • Give it three public properties: $title, $author, and $isAvailable (which should be a boolean).
  • Create a method called checkout(). This method should check if the book is available. If it is, set $isAvailable to false and return a string saying "You have checked out [Title]". If it's already checked out, return "Sorry, [Title] is currently unavailable".
  • Create a method called returnBook() that sets $isAvailable back to true.

Testing your code: Instantiate two different books (e.g., "The Hobbit" and "1984"). Check out the first book twice to ensure your logic prevents double-checkout, and then return it so it can be borrowed again.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.