PHP OOP Properties


In PHP Object-Oriented Programming (OOP), properties are variables that belong to a class. They define the attributes or characteristics of an object and hold the data that an instance of the class can use or modify. Properties are sometimes referred to as member variables or attributes.

Defining Properties

Properties are declared inside a class using the same syntax as variables but with an access modifier (public, protected, or private) that defines their visibility.

Example of Properties in a Class:

class Car { public $color; // Public property protected $model; // Protected property private $engineStatus; // Private property }

In this example, the Car class has three properties:

  • $color: A public property that can be accessed from anywhere.
  • $model: A protected property that can only be accessed within the class and its derived classes.
  • $engineStatus: A private property that can only be accessed within the class itself.

Access Modifiers

Access modifiers determine how a property can be accessed within the code:

  • Public: The property can be accessed from anywhere, both inside and outside the class.
  • Protected: The property can only be accessed within the class itself or by inheriting classes.
  • Private: The property can only be accessed within the class where it is defined.

Example of Using Properties:

class Car { public $color = "red"; // Default value protected $model = "Toyota"; private $engineStatus = "off"; // Method to set the engine status public function startEngine() { $this->engineStatus = "on"; echo "Engine started."; } // Method to get the engine status public function getEngineStatus() { return $this->engineStatus; } } // Create an instance of Car $myCar = new Car(); // Accessing public property echo $myCar->color; // Output: red // Accessing a public method to change private property $myCar->startEngine(); // Output: Engine started. // Accessing a public method to get the value of private property echo $myCar->getEngineStatus(); // Output: on // Directly accessing a protected or private property will cause an error // echo $myCar->model; // Error // echo $myCar->engineStatus; // Error

Accessing Properties from Inside the Class

Inside a class, properties are accessed using the $this keyword followed by the arrow operator (->) and the property name.

class Person { public $name; private $age; // Constructor to initialize properties public function __construct($name, $age) { $this->name = $name; $this->age = $age; } public function introduce() { return "Hi, I'm " . $this->name . " and I'm " . $this->age . " years old."; } } $person1 = new Person("Alice", 25); echo $person1->introduce(); // Output: Hi, I'm Alice and I'm 25 years old.

In this example:

  • $this->name and $this->age are used to access the properties from inside the class.

Getters and Setters

Since protected and private properties cannot be accessed directly from outside the class, getter and setter methods are often used to access and modify these properties in a controlled manner.

  • Getter: A method that retrieves the value of a property.
  • Setter: A method that sets or updates the value of a property.

Example of Getters and Setters:

class BankAccount { private $balance = 0; // Getter for balance public function getBalance() { return $this->balance; } // Setter for balance public function deposit($amount) { if ($amount > 0) { $this->balance += $amount; echo "Deposited: $amount<br>"; } else { echo "Deposit amount must be positive.<br>"; } } } $account = new BankAccount(); $account->deposit(100); // Output: Deposited: 100 echo $account->getBalance(); // Output: 100

Default Values

Properties can be assigned default values when they are declared. If no default value is provided, the property will be NULL by default.

class Product { public $name = "Default Product"; public $price = 0; } $product1 = new Product(); echo $product1->name; // Output: Default Product

Static Properties

Static properties are properties that belong to the class itself rather than an instance of the class. They are declared using the static keyword and can be accessed without creating an instance of the class.

Example of Static Properties:

class Calculator { public static $pi = 3.14; public static function getCircleArea($radius) { return self::$pi * $radius * $radius; } } echo Calculator::$pi; // Output: 3.14 echo Calculator::getCircleArea(10); // Output: 314
  • Static properties are accessed using the scope resolution operator (::).

Summary

  • Properties in PHP OOP are variables that belong to a class and define the attributes of an object.
  • They can have public, protected, or private access modifiers to control their visibility.
  • Properties can be accessed using the $this keyword from inside the class or through getter and setter methods from outside.
  • Static properties belong to the class itself and are accessed using the :: operator.
  • Proper use of properties helps in encapsulating data and controlling access, ensuring that the integrity of the object’s state is maintained.