Content · Web Development · PHP

PHP Class Constants and Visibility

PHP Class Constants and Visibility: Class constants store fixed values on a type and can use public, protected or private visibility. See three examples and important limitations.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Class constants store fixed values on a type and can use public, protected or private visibility.

The examples below begin with the core behavior, then move to an application-oriented use.

How PHP Class Constants and Visibility Works


Class constants store fixed values on a type and can use public, protected or private visibility.

Example 1: Basic Usage


This example uses a small input to make the main behavior visible.

<?php

class HttpStatus { public const int OK = 200; private const int INTERNAL = 999; }
echo HttpStatus::OK;

Output:

200

Class constants store fixed values on a type and can use public, protected or private visibility.

Example 2: A Different Input


A different set of values shows the same operation in another situation.

<?php

class FeatureFlag { protected const bool ENABLED = true; public static function enabled(): bool { return self::ENABLED; } }
var_dump(FeatureFlag::enabled());

Output:

bool(true)

Do not confuse class constants with instance properties; access control applies to constants too.

Practical Example: Applying the Feature


This example shows an application-style use of the feature, with its surrounding setup visible.

<?php

class SecurityPolicy { public const string DEFAULT_ROLE = "reader"; private const int ATTEMPTS = 3; public static function attempts(): int { return self::ATTEMPTS; } }
echo SecurityPolicy::DEFAULT_ROLE . ":" . SecurityPolicy::attempts();

Output:

reader:3

The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.

Important Behavior and Edge Cases


  • Do not confuse class constants with instance properties; access control applies to constants too.
  • Class constants store fixed values on a type and can use public, protected or private visibility.
  • Observe differences in empty inputs, invalid states and optional extension availability where relevant.

Comparison With Related Approaches


ApproachImportant Difference
PHP Class Constants and VisibilityClass constants store fixed values on a type and can use public, protected or private visibility.
Alternative approachEnums are preferable when modeling a closed set of related named states.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring the documented state or precondition of PHP Class Constants and VisibilityDo not confuse class constants with instance properties; access control applies to constants too.
Expecting a different type of resultRead the declared return type and distinguish returned values from changes to the source object.
Assuming a related API works identicallyEnums are preferable when modeling a closed set of related named states.

Key Takeaways


  • Class constants store fixed values on a type and can use public, protected or private visibility.
  • Do not confuse class constants with instance properties; access control applies to constants too.
  • Use the example that matches the values, runtime version and dependencies in your application.

PHP Class Constants and Visibility is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.

Share this resourceSend this page to someone who may find it useful.