Understanding Classes and Instantiation in PHP

KolaKachi
This entry is part 4 of 24 in the series PHP Object-Oriented Programming(OOP) Crash Course

Welcome back, folks! In this guide, we’re going to dive into the world of PHP classes and how to create instances of these classes, also known as instantiation.

What Are Classes and Instantiation?

Before we dive into coding, let’s clarify what classes and instantiation are. Think of a class as a blueprint, a pattern, or a factory that helps you create objects. If you’re building a house or a car, you’d create a blueprint first to describe the structure. Similarly, in programming, a class is a description of an object.

Objects in PHP can be thought of as items or entities within the programming language. While native data types like integers and strings are not strictly objects in PHP, let’s consider them as such for simplicity. Now, if you want to create custom items like “products” for an e-commerce website, you create your own objects with unique behaviors, beyond what integers and strings can do.

Here’s how you create a class in PHP:

class Product {
    // Properties
    public $price = 2;
    public $color = 'green';
    public $total = 0;
    
    // Methods
    public function calculateTotal() {
        return $this->total;
    }
}

In this example, we’ve created a class named Product. It has properties (price, color, and total) and a method (calculateTotal). The properties describe the characteristics of a product, and the method defines what a product can do.

Instantiation: Creating Objects

Now, how do you use this class to create actual product objects? This is where instantiation comes into play. You create instances or copies of the class with specific properties.

$book = new Product();
$phone = new Product();

Here, we’ve created two instances: $book and $phone. These instances inherit all the properties and methods of the Product class. However, the instances are separate from the class itself. Anything you do with an instance has no effect on other instances or the class.

Accessing Properties and Methods

To access properties and methods of an instance, you use the arrow (->) notation. For example:

$book->price; // Access the 'price' property
$book->calculateTotal(); // Call the 'calculateTotal' method

$phone->color; // Access the 'color' property
$phone->calculateTotal(); // Call the 'calculateTotal' method

In the above code, we’re accessing properties like price and calling methods like calculateTotal for both $book and $phone instances.

Changing Instance Values

You can change the values of properties for specific instances without affecting others:

$book->price = 10;

This code updates the price of the $book instance to 10. However, it won’t impact the $phone instance, which still has the default price of 2.

Conclusion

In summary, a class is a blueprint for creating objects, and instantiation is the process of creating instances of that class. Each instance is a separate copy of the class, and changes to one instance do not affect others. This is the foundation of object-oriented programming in PHP.

Series Navigation<< Demystifying the Four Principles of Object-Oriented ProgrammingUnderstanding the $this Keyword in PHP Classes >>

Leave a Reply

Your email address will not be published. Required fields are marked *