Back to Blog
Lesson 25 of the PHP: Modern PHP from the Ground Up course
PHPAugust 12, 20264 min read

PHP Constructors and Properties: Mastering Object Initialization

Master PHP constructors and property visibility to initialize objects with data. Learn to write clean, encapsulated code for your MVC application.

PHPObject Oriented ProgrammingBackend DevelopmentMVC
A close-up view of PHP code displayed on a computer screen, highlighting programming and development concepts.

Previously in this course, we explored Classes and Objects, where we learned how to group related data and behavior into a single structure. In this lesson, we shift from basic definitions to active management: how to ensure every object starts its life in a valid state using constructors and property control.

Understanding the Constructor

When you instantiate a class, you often need that object to hold specific data immediately. Without a constructor, you're forced to manually set properties after instantiation, which leaves a "window of vulnerability" where an object exists but is incomplete or invalid.

A constructor is a special method in PHP named __construct. It is executed automatically the moment you create a new instance of a class using the new keyword.

Think of it as a mandatory setup phase. If your User object requires an email address to function, the constructor ensures that the email is provided right at the moment of creation.

PHP
class User {
    public string $email;

    public function __construct(string $email) {
        $this->email = $email;
    }
}

#6A9955">// The object is guaranteed to have an email immediately
$user = new User("dev@example.com");
echo $user->email; #6A9955">// Outputs: dev@example.com

Managing Property Visibility

In the example above, we used the public keyword for our $email property. This makes the property accessible from anywhere outside the class. While convenient, it’s often dangerous because any part of your code can change that email to an invalid value (like an empty string or an integer) without the object knowing.

Property visibility controls who can see or modify your object's data.

  • public: Accessible from everywhere.
  • private: Only accessible from within the class that defines it.

By marking properties as private, you force other parts of your application to interact with the object only through defined methods (getters and setters), which allows you to validate data before it's stored.

Worked Example: Initializing the Project Model

In our ongoing MVC project, we need a Task model. Instead of just creating an empty task, we want to ensure every task has a title and a completion status upon creation.

PHP
class Task {
    private string $title;
    private bool $isCompleted;

    public function __construct(string $title) {
        $this->title = $title;
        $this->isCompleted = false; #6A9955">// Default state
    }

    public function getTitle(): string {
        return $this->title;
    }

    public function markAsDone(): void {
        $this->isCompleted = true;
    }
}

$task = new Task("Finish the MVC routing module");
echo $task->getTitle(); 

By keeping $title private and providing only a getTitle() method, we ensure that the title cannot be changed accidentally by another piece of code. This is the foundation of encapsulation.

Hands-on Exercise

Create a Product class in a file named Product.php.

  1. Add private properties for $name and $price.
  2. Create a constructor that accepts both values and assigns them to the properties.
  3. Add a getPrice() method that returns the price.
  4. Instantiate the object with "Mechanical Keyboard" and 99.99, then display the price using your method.

Common Pitfalls

  • Forgetting this: Newcomers often forget the $this keyword. Remember, $this refers to the specific instance of the object you are currently working with. You must use $this->propertyName to access properties inside a class.
  • Over-exposing data: Avoid making all properties public. If you don't have a specific reason for an external component to modify a property directly, keep it private.
  • Constructor bloat: Keep constructors focused on setup. If your constructor is doing heavy lifting (like writing to a database or making API calls), you're likely violating the "Single Responsibility Principle."

FAQ

Can a class have multiple constructors? No, PHP does not support constructor overloading (having multiple __construct methods with different parameters). You can, however, use optional parameters in your constructor to achieve similar flexibility.

Why use private if I can just use public? Using private prevents external code from putting your object into an invalid state. It allows you to "gatekeep" data changes, ensuring that your logic inside the class remains reliable.

Recap

We’ve moved from basic object creation to proactive state management. By defining a constructor, we ensure objects are initialized correctly; by using property visibility, we protect the internal state of our data. These steps are essential for building the robust Models we need in our MVC architecture.

Up next: We will dive deeper into Visibility Modifiers to explore protected properties and how they facilitate inheritance in our growing application.

Similar Posts