{"id":1735,"date":"2021-05-18T14:25:08","date_gmt":"2021-05-18T14:25:08","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=1735"},"modified":"2025-04-07T11:34:35","modified_gmt":"2025-04-07T11:34:35","slug":"php-class-constants","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-oop\/php-class-constants\/","title":{"rendered":"PHP Class Constants"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about PHP class constants and how to use them effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-php-class-constants'>Introduction to the PHP class constants <a href=\"#introduction-to-the-php-class-constants\" class=\"anchor\" id=\"introduction-to-the-php-class-constants\" title=\"Anchor for Introduction to the PHP class constants\">#<\/a><\/h2>\n\n\n\n<p>Sometimes, you need to <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-constants\/\">define a constant<\/a> that is specific to a <a href=\"https:\/\/phptutorial.net\/php-oop\/php-objects\/\">class<\/a>. In this case, you can use the PHP class constants.<\/p>\n\n\n\n<p>To define a constant of a class, you use the <code>const<\/code> keyword. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Circle<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">const<\/span> PI = M_PI;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, we define the <code>PI<\/code> constant in the <code>Circle<\/code> class. By convention, a constant name is in uppercase. If the constant name contains multiple words, you can use the underscore (<code>_<\/code>) to separate the words, for example <code>MY_CONSTANT<\/code>.<\/p>\n\n\n\n<p>Since a constant is defined per class, not per instance of the class, you use the <code>self<\/code> keyword to reference the constant inside the class. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Circle<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">const<\/span> PI = M_PI;\n\n    <span class=\"hljs-keyword\">private<\/span> $radius;\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span><span class=\"hljs-params\">(float $radius)<\/span>\n    <\/span>{\n        <span class=\"hljs-keyword\">$this<\/span>-&gt;radius = $radius;\n    }\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">area<\/span><span class=\"hljs-params\">()<\/span>: <span class=\"hljs-title\">float<\/span>\n    <\/span>{\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">self<\/span>::PI * <span class=\"hljs-keyword\">$this<\/span>-&gt;radius ** <span class=\"hljs-number\">2<\/span>;\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmNsYXNzIENpcmNsZQp7CiAgICBjb25zdCBQSSA9IE1fUEk7CgogICAgcHJpdmF0ZSAkcmFkaXVzOwoKICAgIHB1YmxpYyBmdW5jdGlvbiBfX2NvbnN0cnVjdChmbG9hdCAkcmFkaXVzKQogICAgewogICAgICAgICR0aGlzLT5yYWRpdXMgPSAkcmFkaXVzOwogICAgfQoKICAgIHB1YmxpYyBmdW5jdGlvbiBhcmVhKCk6IGZsb2F0CiAgICB7CiAgICAgICAgcmV0dXJuIHNlbGY6OlBJICogJHRoaXMtPnJhZGl1cyAqKiAyOwogICAgfQp9\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>In this example, we define the <code>Circle<\/code> class with the <code>$radius<\/code> property. Inside the <code>area()<\/code> method, we calculate the area of the circle using the radius and the <code>self::PI<\/code> constant.<\/p>\n\n\n\n<p>When you define a constant in a class, its visibility is public by default. It means that you can also access the constant outside of the class.<\/p>\n\n\n\n<p>To reference to the constant outside the class, you use the class name and <code>::<\/code> operator like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-comment\">\/\/  Circle class<\/span>\n<span class=\"hljs-comment\">\/\/  ...<\/span>\n\n<span class=\"hljs-keyword\">echo<\/span> Circle::PI;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that you can reference the class using a variable with the value is the class name. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$className = <span class=\"hljs-string\">'Circle'<\/span>;\n\n<span class=\"hljs-keyword\">echo<\/span> $className::PI;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmNsYXNzIENpcmNsZQp7CiAgICBjb25zdCBQSSA9IE1fUEk7CgogICAgcHJpdmF0ZSAkcmFkaXVzOwoKICAgIHB1YmxpYyBmdW5jdGlvbiBfX2NvbnN0cnVjdChmbG9hdCAkcmFkaXVzKQogICAgewogICAgICAgICR0aGlzLT5yYWRpdXMgPSAkcmFkaXVzOwogICAgfQoKICAgIHB1YmxpYyBmdW5jdGlvbiBhcmVhKCk6IGZsb2F0CiAgICB7CiAgICAgICAgcmV0dXJuIHNlbGY6OlBJICogJHRoaXMtPnJhZGl1cyAqKiAyOwogICAgfQp9CgokY2xhc3NOYW1lID0gJ0NpcmNsZSc7CgplY2hvICRjbGFzc05hbWU6OlBJOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>In this example, instead of using <code>Circle::PI<\/code>, we use the <code>$className::PI<\/code> to reference the PI constant.<\/p>\n\n\n\n<p>Since PHP 7.1.0, you can use visibility modifier keywords such as <code>private<\/code>, <code>protected<\/code>, and <code>public<\/code> with the class constant. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Circle<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">const<\/span> PI = M_PI;\n\n    <span class=\"hljs-comment\">\/\/ other methods   <\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the <code>PI<\/code> constant is private and cannot be used outside the <code>Circle<\/code> class.<\/p>\n\n\n\n<p>It&#8217;s possible to define a class constant using a constant expression. A constant expression is an expression that contains only constants. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MyClass<\/span> \n<\/span>{\n    <span class=\"hljs-keyword\">const<\/span> ONE_THIRD = <span class=\"hljs-number\">1<\/span>\/<span class=\"hljs-number\">3<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='php-class-constants-and-inheritance'>PHP class constants and inheritance <a href=\"#php-class-constants-and-inheritance\" class=\"anchor\" id=\"php-class-constants-and-inheritance\" title=\"Anchor for PHP class constants and inheritance\">#<\/a><\/h2>\n\n\n\n<p>The following example illustrates how to define a constant in the parent class and override it in the child class. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">abstract<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Model<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-keyword\">const<\/span> TABLE_NAME = <span class=\"hljs-string\">''<\/span>;\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">all<\/span><span class=\"hljs-params\">()<\/span>\n    <\/span>{\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">'SELECT * FROM '<\/span> . <span class=\"hljs-keyword\">static<\/span>::TABLE_NAME;\n    }\n}\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">User<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">Model<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-keyword\">const<\/span> TABLE_NAME = <span class=\"hljs-string\">'users'<\/span>;\n}\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Role<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">Model<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-keyword\">const<\/span> TABLE_NAME = <span class=\"hljs-string\">'roles'<\/span>;\n}\n\n<span class=\"hljs-keyword\">echo<\/span> User::all() . <span class=\"hljs-string\">'&lt;br&gt;'<\/span>; <span class=\"hljs-comment\">\/\/ SELECT * FROM users;<\/span>\n<span class=\"hljs-keyword\">echo<\/span> Role::all() . <span class=\"hljs-string\">'&lt;br&gt;'<\/span>; <span class=\"hljs-comment\">\/\/ SELECT * FROM roles;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmFic3RyYWN0IGNsYXNzIE1vZGVsCnsKICAgIHByb3RlY3RlZCBjb25zdCBUQUJMRV9OQU1FID0gJyc7CgogICAgcHVibGljIHN0YXRpYyBmdW5jdGlvbiBhbGwoKQogICAgewogICAgICAgIHJldHVybiAnU0VMRUNUICogRlJPTSAnIC4gc3RhdGljOjpUQUJMRV9OQU1FOwogICAgfQp9CgpjbGFzcyBVc2VyIGV4dGVuZHMgTW9kZWwKewogICAgcHJvdGVjdGVkIGNvbnN0IFRBQkxFX05BTUUgPSAndXNlcnMnOwp9CgpjbGFzcyBSb2xlIGV4dGVuZHMgTW9kZWwKewogICAgcHJvdGVjdGVkIGNvbnN0IFRBQkxFX05BTUUgPSAncm9sZXMnOwp9CgplY2hvIFVzZXI6OmFsbCgpIC4gJzxicj4nOyAvLyBTRUxFQ1QgKiBGUk9NIHVzZXJzOwplY2hvIFJvbGU6OmFsbCgpIC4gJzxicj4nOyAvLyBTRUxFQ1QgKiBGUk9NIHJvbGVzOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, define a constant <code>TABLE_NAME<\/code> in the <code>Model<\/code> class. In the <code>all()<\/code> static method, returns a query that selects all rows from the table name specified by the <code>TABLE_NAME<\/code> constant.<\/p>\n\n\n\n<p>Second, define the <code>User<\/code> and <code>Role<\/code> classes that extend the <code>Model<\/code> class. In the User and Role class, redefine the <code>TABLE_NAME<\/code> constant.<\/p>\n\n\n\n<p>Since the <code>User<\/code> and <code>Role<\/code> classes inherits the <code>all()<\/code> method of the <code>Model<\/code> class, they can call the <code>all()<\/code> method.<\/p>\n\n\n\n<p>When the <code>User<\/code> class calls the <code>all()<\/code> method, the <code>all()<\/code> method returns the expected <code>TALBE_NAME<\/code> constant defined in the <code>User<\/code> class. The same logic is applied to the <code>Role<\/code> class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>const<\/code> keyword to define a class constant.<\/li>\n\n\n\n<li>Use the visibility modifier keywords, including public, protected, and private. By default, a class constant is public.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1735\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-oop\/php-class-constants\/\"\n\t\t\t\tdata-post-title=\"PHP Class Constants\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1735\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-oop\/php-class-constants\/\"\n\t\t\t\tdata-post-title=\"PHP Class Constants\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn about PHP class constants and how to use them effectively.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1753,"menu_order":16,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1735","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1735","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=1735"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1735\/revisions"}],"predecessor-version":[{"id":3233,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1735\/revisions\/3233"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1753"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=1735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}