Uncategorized

PHP – Class Constants

Class constants are unchanging values declared within a class using the const keyword. Unlike regular properties, class constants:

Cannot be modified once declared.
Are shared among all instances of the class (static in nature).
Are accessed using the scope resolution operator (::).
Declaring and Accessing Class Constants
Declaration:

Use the const keyword.
By convention, constants are named in uppercase letters.
Access:

From outside the class: Use ClassName::CONSTANT_NAME.
From inside the class: Use self::CONSTANT_NAME.

Example 1: Using Class Constants

<?php
class Company {
  const COMPANY_NAME = "Tech Innovations Inc.";
  const COMPANY_MOTTO = "Innovate, Inspire, Achieve";

  public function displayDetails() {
    echo "Welcome to " . self::COMPANY_NAME . ". Our motto: " . self::COMPANY_MOTTO;
  }
}

// Access constants from outside the class
echo "Company Name: " . Company::COMPANY_NAME . "<br>";
echo "Motto: " . Company::COMPANY_MOTTO . "<br>";

// Access constants from inside the class
$company = new Company();
$company->displayDetails();
?>

Output:

Company Name: Tech Innovations Inc.
Motto: Innovate, Inspire, Achieve
Welcome to Tech Innovations Inc. Our motto: Innovate, Inspire, Achieve
Example 2: Class Constants in Inheritance
Constants are not affected by inheritance in the same way as methods or properties. However, a child class can define its own constants with the same name as the parent class constants (overriding is not possible but redefining is).

<?php
class ParentClass {
  const MESSAGE = "This is the parent class.";
}

class ChildClass extends ParentClass {
  const MESSAGE = "This is the child class.";
}

// Access constants
echo ParentClass::MESSAGE . "<br>"; // Outputs: This is the parent class.
echo ChildClass::MESSAGE . "<br>"; // Outputs: This is the child class.
?>

Benefits of Class Constants
Immutability:

Prevent accidental modification of constant values.
Better Performance:

Constants are faster than variables as they don’t require memory allocation.
Readability and Maintenance:

Use constants for values that should remain the same, such as configurations, fixed messages, or thresholds.

Example 3: Real-Life Scenario – Configurations

<?php
class Config {
  const DB_HOST = "localhost";
  const DB_NAME = "my_database";
  const DB_USER = "root";
  const DB_PASSWORD = "password";

  public static function getDbConnectionDetails() {
    return "Connecting to " . self::DB_HOST . ", Database: " . self::DB_NAME;
  }
}

// Access configuration constants
echo Config::DB_HOST . "<br>"; // Outputs: localhost
echo Config::getDbConnectionDetails(); // Outputs: Connecting to localhost, Database: my_database
?>

Conclusion
Class constants are a powerful tool in PHP, ideal for storing immutable values that are used across the class or application. They simplify code readability and ensure values remain consistent throughout the application.

Leave a Reply

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