PHP
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Object-Oriented PHP
-
Section 5: Working with Data
-
Section 6: Modern PHP (PHP 8)
-
Section 7: Working with Files and Networking
-
Section 8: Common PHP Frameworks Overview
-
Section 9: Tooling and Ecosystem
-
Section 10: Practical Projects
-
Section 11: Interview Practice
-
Section 12: More Standard Library
-
Section 13: Data Structures and Algorithms in PHP
-
Section 14: More Practice Exercises
-
Section 15: More Security and Best Practices
-
Section 16: More Testing and Tooling
-
Section 17: WordPress-Style CMS Concepts
-
Section 18: Advanced OOP Practice
-
Section 19: More Web Fundamentals
-
Section 20: Database Practice
-
Section 21: PHP Manual: Array Functions
-
Section 22: PHP Manual: Date and Calendar Functions
-
Section 23: PHP Manual: Filesystem and Directory Functions
-
Section 24: PHP Manual: Filter and Var Handling
-
Section 25: PHP Manual: Math Functions
-
Section 26: PHP Manual: JSON and XML
-
Section 27: PHP Manual: Network and Stream Functions
-
Section 28: PHP Manual: Error and Exception Handling
-
Section 29: PHP Manual: Output Control and Misc
-
Section 30: PHP Manual: FTP, Zip, and Mail
-
Section 31: Modern PHP Frameworks Deep Dive
-
Section 32: PHP Design Patterns
-
Section 33: More Practice Exercises
-
Section 34: PHP Performance and Deployment
-
Section 35: More Interview Practice
-
Section 36: More PHP Standard Library
-
Section 37: PHP Concurrency and Async
-
Section 38: More Web Development Practice
-
Section 39: PHP Testing Deep Dive
-
Section 40: Composer and Package Development
-
Section 41: PHP Security Deep Dive
-
Section 42: More Practical Projects
-
Section 43: Legacy PHP Maintenance
-
Section 44: More Algorithm Practice
-
Section 45: Final Practice and Review
-
Section 46: PHP for E-Commerce Patterns
-
Section 47: PHP API Design Deep Dive
-
Section 48: PHP Caching Strategies
-
Section 49: PHP Queue and Background Jobs
-
Section 50: PHP Multi-Tenancy Patterns
-
Section 51: PHP Real-Time Features
-
Section 52: PHP CMS and Content Modeling
-
Section 53: PHP Internationalization
-
Section 54: More Framework-Specific Practice
-
Section 55: PHP Legacy Code Refactoring
-
Section 56: More Practice Projects Round 2
-
Section 57: PHP Command-Line Applications
-
Section 58: PHP and Microservices
-
Section 59: More Interview and Review Round 2
16: Classes and Objects
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$isAvailabletofalseand 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$isAvailableback totrue.
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.
There are no comments for now.