Understanding Accessors in Object-Oriented Programming

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

Welcome to our exploration of accessors in object-oriented programming. In this blog post, we’ll delve into the significance of “public,” “private,” “protected,” and “static” accessors, why they matter, and when to use them in your code.

The Purpose of Accessors

Accessors serve as a vital part of object-oriented programming, helping to control the visibility and accessibility of class members. We start by examining “private” accessors. When a class member is marked as “private,” it signifies that it cannot be accessed from outside the class. This encapsulation ensures that the class remains self-contained and reveals only what’s necessary to the outside world.

Example:

class ShoppingCart {
    private $price;

    public function calculateTotal() {
        // This function can access the private $price
        return $this->price;
    }
}

$cart = new ShoppingCart();
// Cannot access $price directly from outside
// $cart->price; // Results in an error

Public Accessors

On the other side, we have “public” accessors, which are accessible both from within and outside the class. Public members are used when you want a class member to be easily accessible to external code, without any restrictions.

Example:

class Product {
    public $name;

    public function displayInfo() {
        echo "Product Name: " . $this->name;
    }
}

$product = new Product();
$product->name = "Example Product";
$product->displayInfo(); // Accessing public member from external code

Protected Accessors

Now, let’s discuss “protected” accessors. While they might seem similar to private members, protected members have a crucial difference: they can be inherited by child classes. This allows child classes to access and use protected members much like private ones.

Example:

class Vehicle {
    protected $fuelType;

    protected function startEngine() {
        echo "Engine started";
    }
}

class Car extends Vehicle {
    public function getFuelType() {
        return $this->fuelType; // Accessing protected member from a child class
    }
}

Static Accessors

The “static” accessor designates members associated with the class itself, rather than instances of the class. Static members are accessed using the class name and are used to maintain data or functionality shared across all instances of the class.

Example:

class Counter {
    public static $count = 0;

    public function increment() {
        self::$count++; // Accessing a static variable using self
    }

    public static function getCount() {
        return self::$count; // Accessing a static variable using self
    }
}

$counter1 = new Counter();
$counter1->increment();
echo Counter::getCount(); // Accessing a static method using the class name

Conclusion

Accessors play a crucial role in designing well-structured and maintainable code. Whether you’re enforcing encapsulation, ensuring data integrity, or designing class hierarchies, choosing the right accessors is key to success. The next time you’re working with classes, carefully consider which accessors are appropriate for your members to create an organized and secure codebase.

In our next article, we’ll delve deeper into static members and explore their unique role in object-oriented programming.

Series Navigation<< Understanding the $this Keyword in PHP ClassesDemystifying PHP Static and Self Keywords >>

Leave a Reply

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