As a developer, you'll often need to work with PHP class constants to ensure your code is efficient and maintainable. PHP offers two types of constants: global constants, defined using the defining construct, and class constants, defined using the keyword const within a class. The latter was introduced in PHP 7.1.0 and is essential for any PHP engineer to understand.
A constant represents a value that cannot be changed once set, acting as an immutable variable. In object-oriented programming, class constants are particularly useful when you need to define fixed values within a class. This guide will explore how to use public, private, and protected PHP class constants, making it easier to manage constant data in your object-oriented development.
General Details of PHP Class Constants
Like typical constants in any language, PHP class constants cannot be changed once declared. Here are the general properties associated with all types of PHP class constants:
- Class constants are useful when you need to describe some constant data within a class.
- You only need to allocate memory to each class constant once per class and not for every class instance.
- To declare a class constant, use the const keyword inside a class.
- The name you choose for a PHP class constant cannot be a keyword such as self, parent, or static. These are important details to focus on when hiring PHP developers.
- Class constants are case-sensitive. There’s no luxury here; class constants must be declared only in uppercase as a convention. The immutable nature of constants is why their name should stand out in your code. This standard specifies that any class constant name is fully capitalized, so it's best to stick with that.
- Class constants are accessible outside the class by using the scope resolution operator –:: – and the constant's name after the class name. Below is an example of this:
class SayHello {
const MORNING_MESSAGE = ‘Hello and Good morning! This is Serendipity Radio! We’re pleased to talk with you.’;
}
echo SayHello::MORNING_MESSAGE;
?>
In another example, you can access a constant from inside the class using the self keyword followed by the scope resolution operator (again, that’s::) and the constant name (in that order).
Notice that we use an underscore as a separator where the class constant's name contains multiple words. A web developer should know these.
How Public, Private, and Protected Class Constants are Different
PHP 7.1.0 introduced us to visibility modifier keywords, including public, private, and protected with the class constant. The default visibility of any class constant is public even when it’s not specified, meaning the class constant was not declared with an explicit visibility modifier.
Are you public or private?
Here’s one example of creating a class constant in PHP:
// your methods
}
?>
From the example above, notice you don't need a dollar sign ($) to precede the class constant's name as you do for normal variables.
The PI constant is private. What does mean? It means there’s no way to use it outside the Circle class.
You can also do a PHP class constant using a constant expression, which is an expression consisting of only constants. Here’s one example:
// your methods
}
?>
Next, we must understand what happens when we use public, private, and protected in WordPress development where PHP use is prevalent..
echo Test::PUBLIC_CLASS_CONSTANT;
echo Test::PRIVATE_CLASS_CONSTANT;
?>
Here’s the output of the code above:
This is public, so you see it!
Fatal error: Uncaught Error: Cannot access private const Test::PRIVATE_CLASS_CONSTANT…
If you don’t understand what’s going on in the code above, here’s some clear line-by-line explanation:
- class Test declares the class named Test.
- public const PUBLIC_CLASS_CONSTANT = ‘This is public, so you see it!’; declares a public class constant named PUBLIC_CLASS_CONSTANT and which holds a specific message ‘This is public, so you see it!’
- public const PUBLIC_CLASS_CONSTANT = ‘This is public, so you see it!’; declares a public class constant named PUBLIC_CLASS_CONSTANT and which holds a specific message This is public, so you see it!
- The line echo Test::PUBLIC_CLASS_CONSTANT prints the text This is public, so you see it! to your screen.
- The line echo Test::PRIVATE_CLASS_CONSTANT is expected to print the text “This is private; you’ll see an error instead.” to your screen. However, you won’t see that message. Instead, you’ll see the following scary error message from the unforgiving PHP interpreter: Fatal error: Uncaught Error: Cannot access private const Test::PRIVATE_CLASS_CONSTANT…
Notice that the error does not happen with the public class constant but with the private class constant. The PHP interpreter's message says: Cannot access private const Test::PRIVATE_CLASS_CONSTANT. Therefore, private class constants are not accessible from outside the class.
How protected are you?
Having looked at public and private class constants, we’ll now turn our attention to protected class constants.
The protected modifier is tricky for new developers because it offers less visibility than public but more than private. Therefore, it is more secure than the public access modifier. Here are a few more general properties of the protected visibility modifier:
- External classes cannot access protected class constants; only subclasses can.
- protected class constants are public to their containing class (where they are declared) and a child class that inherits this class constant from the parent class.
- protected PHP class constants have a second level of security, one level lower than private (the most secure) but more than public which is not so secure. (This point is worth repeating!)
- Declaring class constants as protected helps you describe which ones you want to make shareable and those you'd rather keep within the confines of a class.
Now, we can modify the code from the previous example:
// Declare a protected class constant
protected const PROTECTED_CLASS_CONSTANT = ‘I am protected; you may not see me everywhere. Yikes!’;
}
echo Test::PUBLIC_CLASS_CONSTANT;
echo Test::PRIVATE_CLASS_CONSTANT;
// Print the protected class constant
echo Test::PROTECTED_CLASS_CONSTANT;
?>
When we run the code above, we get the following output:
// Output for public class constant
This is public, so you see it!
// Output for private class constant
Fatal error: Uncaught Error: Cannot access private const
// Output for protected class constant
Fatal error: Uncaught Error: Cannot access protected const Test::PROTECTED_CLASS_CONSTANT in…
What happened here? We tried to access a protected PHP class constant, and it inevitably blew up in our faces! That’s what happens when you control the visibility of a class constant with protected.
FAQs:
Q1. What is a protected function PHP?
A protected function PHP is a function that is only accessible from its containing class. It is also only accessible by classes derived from that class. Protected functions are common in e-commerce web development.
Q2. What is a public function PHP?
A PHP public function is a function that works or is accessible outside the class it's declared in. It ensures that the entire content in its class is accessible to another class only during access. An unspecified visibility modifier for a function defaults to public.
Q3. What is private in OOP?
In object-oriented development, private means that a specific class member can only be accessed from within the class in question. Therefore, you need public methods to access private class members. These methods are called getters and setters to interact with private properties.