{"id":27279,"date":"2017-01-30T16:10:49","date_gmt":"2017-01-30T14:10:49","guid":{"rendered":"https:\/\/mamchenkov.net\/wordpress\/?p=27279"},"modified":"2017-01-30T16:10:49","modified_gmt":"2017-01-30T14:10:49","slug":"sharing-constants-between-php-classes","status":"publish","type":"post","link":"https:\/\/mamchenkov.net\/wordpress\/2017\/01\/30\/sharing-constants-between-php-classes\/","title":{"rendered":"Sharing constants between PHP classes"},"content":{"rendered":"<!-- google_ad_section_start -->\n<p>When writing larger applications, it is often useful to have some constants defined, which can then be shared between different parts of the application. \u00a0There are several ways to do this, and there is no real rocket science here.<\/p>\n<p>However, the question is: what&#8217;s the best way to do so?<\/p>\n<p>Until now I haven&#8217;t seen an elegant solution to this problem. \u00a0The old school way was defining the constants in the separate include file or in a class, and then accessing them through the global scope or via a specific class. \u00a0With a moderately modern PHP version, class constants can also be redefined by an inheriting class,\u00a0which is, I think, more useful than painful.<\/p>\n<p>Today, I came across an implementation that I really liked. \u00a0It&#8217;s simple and elegant: define constants in the interface and let classes implement it. \u00a0I saw it in the <a href=\"https:\/\/github.com\/php-fig\/http-message-util\">HTTP Message Util<\/a> library, which defines HTTP methods, status codes and the like for the libraries and applications implementing <a href=\"http:\/\/www.php-fig.org\/psr\/psr-7\/\">PSR-7 standard<\/a> (HTTP message interface). \u00a0<a href=\"https:\/\/github.com\/php-fig\/http-message-util\/blob\/master\/src\/RequestMethodInterface.php\">Look here<\/a>, for example.<\/p>\n<p>I played around for a few minutes with this and it worked really well. \u00a0Until I stumbled upon\u00a0something. \u00a0PHP classes can override the constants from the classes they inherit. \u00a0But not from the interface. \u00a0Here&#8217;s what PHP manual on <a href=\"http:\/\/php.net\/manual\/en\/language.oop5.interfaces.php#language.oop5.interfaces.constants\">Object Interfaces<\/a> has to say about that:<\/p>\n<blockquote><p>It&#8217;s possible for interfaces to have constants. Interface constants works exactly like <a class=\"link\" href=\"http:\/\/php.net\/manual\/en\/language.oop5.constants.php\">class constants<\/a> except they cannot be overridden by a class\/interface that inherits them.<\/p><\/blockquote>\n<p>This seems reasonable. \u00a0After all, the interface is a contract, and whatever is part of it should be precisely so in the implementing classes. \u00a0If you try to redefine the constants in the class:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\ninterface FooInterface\r\n{\r\n    const FOO = 1;\r\n}\r\n\r\nclass Foo implements FooInterface\r\n{\r\n    const FOO = 1;\r\n}\r\n\r\necho &quot;Foo = &quot; . Foo::FOO . &quot;\\n&quot;;\r\n<\/pre>\n<p>you get an error:<\/p>\n<blockquote><p><strong>PHP Fatal error<\/strong>: Cannot inherit previously-inherited or override constant FOO from interface FooInterface in foo.php on line 7<\/p><\/blockquote>\n<p>That sounds about right. \u00a0But! \u00a0It looks like you can overwrite the interface constants by extending the class. \u00a0The following snippet happily prints out two and throws no errors or warnings (my Fedora 25 runs PHP 7.0.14, if you were wondering):<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\ninterface FooInterface\r\n{\r\n    const FOO = 1;\r\n}\r\n\r\nclass Foo implements FooInterface\r\n{\r\n}\r\nclass Bar extends Foo\r\n{\r\n    const FOO = 2;\r\n}\r\n\r\necho &quot;Foo = &quot; . Bar::FOO . &quot;\\n&quot;;\r\n<\/pre>\n<p>Weird, right? \u00a0But there is more. \u00a0If the extending class implements the interface, rather than inherit its implementation from the parent, then the error is back:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\ninterface FooInterface\r\n{\r\n    const FOO = 1;\r\n}\r\n\r\nclass Foo implements FooInterface\r\n{\r\n}\r\nclass Bar extends Foo implements FooInterface\r\n{\r\n    const FOO = 2;\r\n}\r\n\r\necho &quot;Foo = &quot; . Bar::FOO . &quot;\\n&quot;;\r\n<\/pre>\n<p>results in the familiar error, with the adjusted line number:<\/p>\n<blockquote><p><strong>PHP Fatal error<\/strong>: Cannot inherit previously-inherited or override constant FOO from interface FooInterface in foo.php on line 10<\/p><\/blockquote>\n<p>Additionally,\u00a0PHP provides the <a href=\"http:\/\/php.net\/manual\/en\/language.oop5.final.php\">final keyword<\/a>, which can be used to prevent overwriting of the inherited method by the child classes. \u00a0However, final keyword is not allowed on the constants, which makes results a bit more difficult to predict. \u00a0Especially so with the late static binding. \u00a0Here is an example:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\ninterface FooInterface\r\n{\r\n    const FOO = 1;\r\n\r\n    public function hello();\r\n}\r\n\r\nclass Foo implements FooInterface\r\n{\r\n    public function hello()\r\n    {   \r\n        echo __FUNCTION__ . &quot;: Self &quot; . self::FOO . &quot;\\n&quot;;\r\n        echo __FUNCTION__ . &quot;: Static &quot; . static::FOO . &quot;\\n&quot;;\r\n    }   \r\n\r\n    final public function bye()\r\n    {   \r\n        echo __FUNCTION__ . &quot;: Self &quot; . self::FOO . &quot;\\n&quot;;\r\n        echo __FUNCTION__ . &quot;: Static &quot; . static::FOO . &quot;\\n&quot;;\r\n    }   \r\n}\r\nclass Bar extends Foo \r\n{\r\n    const FOO = 2;\r\n}\r\n\r\necho &quot;\\nFrom Foo\\n&quot;;\r\n$foo = new Foo();\r\n$foo-&gt;hello();\r\n$foo-&gt;bye();\r\n\r\necho &quot;\\nFrom Bar\\n&quot;;\r\n$bar = new Bar();\r\n$bar-&gt;hello();\r\n$bar-&gt;bye();\r\n\r\n<\/pre>\n<p>Which results in:<\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\n\r\nFrom Foo\r\nhello: Self 1\r\nhello: Static 1\r\nbye: Self 1\r\nbye: Static 1\r\n\r\nFrom Bar\r\nhello: Self 1\r\nhello: Static 2\r\nbye: Self 1\r\nbye: Static 2\r\n\r\n<\/pre>\n<p>I can&#8217;t say for sure whether overwriting interface constants in inherited classes is a bug or not, but the current behavior does feel weird.<\/p>\n<!-- google_ad_section_end -->\n","protected":false},"excerpt":{"rendered":"<!-- google_ad_section_start -->\n<p>When writing larger applications, it is often useful to have some constants defined, which can then be shared between different parts of the application. \u00a0There are several ways to do this, and there is no real rocket science here. However, the question is: what&#8217;s the best way to do so? Until now I haven&#8217;t seen &hellip; <a href=\"https:\/\/mamchenkov.net\/wordpress\/2017\/01\/30\/sharing-constants-between-php-classes\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Sharing constants between PHP classes<\/span><\/a><\/p>\n<!-- google_ad_section_end -->\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Sharing constants between PHP classes #PHP #WebDev","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false,"_links_to":"","_links_to_target":""},"categories":[1,18,62,1334],"tags":[3069,38,1330],"keyring_services":[],"class_list":["post-27279","post","type-post","status-publish","format-standard","hentry","category-general","category-programming","category-technology","category-web-work","tag-best-practices","tag-php","tag-web-development"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.8 - aioseo.com -->\n\t<meta name=\"description\" content=\"When writing larger applications, it is often useful to have some constants defined, which can then be shared between different parts of the application. There are several ways to do this, and there is no real rocket science here. However, the question is: what&#039;s the best way to do so? Until now I haven&#039;t seen\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Leonid Mamchenkov\"\/>\n\t<meta name=\"google-site-verification\" content=\"VHvdD0_usx1_4DzKy_QCVcICVgX2EgA2ybELT-wl7kQ\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/mamchenkov.net\/wordpress\/2017\/01\/30\/sharing-constants-between-php-classes\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.8\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Leonid Mamchenkov - Life, universe, and everything else\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Sharing constants between PHP classes - Leonid Mamchenkov\" \/>\n\t\t<meta property=\"og:description\" content=\"When writing larger applications, it is often useful to have some constants defined, which can then be shared between different parts of the application. There are several ways to do this, and there is no real rocket science here. However, the question is: what&#039;s the best way to do so? Until now I haven&#039;t seen\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/mamchenkov.net\/wordpress\/2017\/01\/30\/sharing-constants-between-php-classes\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2026\/03\/leonid-sailing-beer.jpg\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2026\/03\/leonid-sailing-beer.jpg\" \/>\n\t\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t\t<meta property=\"og:image:height\" content=\"1024\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2017-01-30T14:10:49+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2017-01-30T14:10:49+00:00\" \/>\n\t\t<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/MamchenkovBlog\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@mamchenkov\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Sharing constants between PHP classes - Leonid Mamchenkov\" \/>\n\t\t<meta name=\"twitter:description\" content=\"When writing larger applications, it is often useful to have some constants defined, which can then be shared between different parts of the application. There are several ways to do this, and there is no real rocket science here. However, the question is: what&#039;s the best way to do so? Until now I haven&#039;t seen\" \/>\n\t\t<meta name=\"twitter:creator\" content=\"@mamchenkov\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2026\/03\/leonid-sailing-beer.jpg\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2017\\\/01\\\/30\\\/sharing-constants-between-php-classes\\\/#blogposting\",\"name\":\"Sharing constants between PHP classes - Leonid Mamchenkov\",\"headline\":\"Sharing constants between PHP classes\",\"author\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/author\\\/leonid\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/#person\"},\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2017\\\/01\\\/30\\\/sharing-constants-between-php-classes\\\/#articleImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3cf6df002a284d78fb6e9d8222ca4d102e0832035ed6bc8447008bd234e131a4?s=96&d=identicon&r=g\",\"width\":96,\"height\":96,\"caption\":\"Leonid Mamchenkov\"},\"datePublished\":\"2017-01-30T16:10:49+02:00\",\"dateModified\":\"2017-01-30T16:10:49+02:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2017\\\/01\\\/30\\\/sharing-constants-between-php-classes\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2017\\\/01\\\/30\\\/sharing-constants-between-php-classes\\\/#webpage\"},\"articleSection\":\"All, Programming, Technology, Web work, best practices, PHP, web development\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2017\\\/01\\\/30\\\/sharing-constants-between-php-classes\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/#listItem\",\"position\":2,\"name\":\"Technology\",\"item\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/programming\\\/#listItem\",\"name\":\"Programming\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/programming\\\/#listItem\",\"position\":3,\"name\":\"Programming\",\"item\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/programming\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2017\\\/01\\\/30\\\/sharing-constants-between-php-classes\\\/#listItem\",\"name\":\"Sharing constants between PHP classes\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2017\\\/01\\\/30\\\/sharing-constants-between-php-classes\\\/#listItem\",\"position\":4,\"name\":\"Sharing constants between PHP classes\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/programming\\\/#listItem\",\"name\":\"Programming\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/#person\",\"name\":\"Leonid Mamchenkov\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2017\\\/01\\\/30\\\/sharing-constants-between-php-classes\\\/#personImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3cf6df002a284d78fb6e9d8222ca4d102e0832035ed6bc8447008bd234e131a4?s=96&d=identicon&r=g\",\"width\":96,\"height\":96,\"caption\":\"Leonid Mamchenkov\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/author\\\/leonid\\\/#author\",\"url\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/author\\\/leonid\\\/\",\"name\":\"Leonid Mamchenkov\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2017\\\/01\\\/30\\\/sharing-constants-between-php-classes\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3cf6df002a284d78fb6e9d8222ca4d102e0832035ed6bc8447008bd234e131a4?s=96&d=identicon&r=g\",\"width\":96,\"height\":96,\"caption\":\"Leonid Mamchenkov\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2017\\\/01\\\/30\\\/sharing-constants-between-php-classes\\\/#webpage\",\"url\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2017\\\/01\\\/30\\\/sharing-constants-between-php-classes\\\/\",\"name\":\"Sharing constants between PHP classes - Leonid Mamchenkov\",\"description\":\"When writing larger applications, it is often useful to have some constants defined, which can then be shared between different parts of the application. There are several ways to do this, and there is no real rocket science here. However, the question is: what's the best way to do so? Until now I haven't seen\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2017\\\/01\\\/30\\\/sharing-constants-between-php-classes\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/author\\\/leonid\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/author\\\/leonid\\\/#author\"},\"datePublished\":\"2017-01-30T16:10:49+02:00\",\"dateModified\":\"2017-01-30T16:10:49+02:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/#website\",\"url\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/\",\"name\":\"Blog of Leonid Mamchenkov\",\"description\":\"Life, universe, and everything else\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/#person\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Sharing constants between PHP classes - Leonid Mamchenkov","description":"When writing larger applications, it is often useful to have some constants defined, which can then be shared between different parts of the application. There are several ways to do this, and there is no real rocket science here. However, the question is: what's the best way to do so? Until now I haven't seen","canonical_url":"https:\/\/mamchenkov.net\/wordpress\/2017\/01\/30\/sharing-constants-between-php-classes\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"google-site-verification":"VHvdD0_usx1_4DzKy_QCVcICVgX2EgA2ybELT-wl7kQ","miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/mamchenkov.net\/wordpress\/2017\/01\/30\/sharing-constants-between-php-classes\/#blogposting","name":"Sharing constants between PHP classes - Leonid Mamchenkov","headline":"Sharing constants between PHP classes","author":{"@id":"https:\/\/mamchenkov.net\/wordpress\/author\/leonid\/#author"},"publisher":{"@id":"https:\/\/mamchenkov.net\/wordpress\/#person"},"image":{"@type":"ImageObject","@id":"https:\/\/mamchenkov.net\/wordpress\/2017\/01\/30\/sharing-constants-between-php-classes\/#articleImage","url":"https:\/\/secure.gravatar.com\/avatar\/3cf6df002a284d78fb6e9d8222ca4d102e0832035ed6bc8447008bd234e131a4?s=96&d=identicon&r=g","width":96,"height":96,"caption":"Leonid Mamchenkov"},"datePublished":"2017-01-30T16:10:49+02:00","dateModified":"2017-01-30T16:10:49+02:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/mamchenkov.net\/wordpress\/2017\/01\/30\/sharing-constants-between-php-classes\/#webpage"},"isPartOf":{"@id":"https:\/\/mamchenkov.net\/wordpress\/2017\/01\/30\/sharing-constants-between-php-classes\/#webpage"},"articleSection":"All, Programming, Technology, Web work, best practices, PHP, web development"},{"@type":"BreadcrumbList","@id":"https:\/\/mamchenkov.net\/wordpress\/2017\/01\/30\/sharing-constants-between-php-classes\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress#listItem","position":1,"name":"Home","item":"https:\/\/mamchenkov.net\/wordpress","nextItem":{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/#listItem","name":"Technology"}},{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/#listItem","position":2,"name":"Technology","item":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/programming\/#listItem","name":"Programming"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/programming\/#listItem","position":3,"name":"Programming","item":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/programming\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/2017\/01\/30\/sharing-constants-between-php-classes\/#listItem","name":"Sharing constants between PHP classes"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/#listItem","name":"Technology"}},{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/2017\/01\/30\/sharing-constants-between-php-classes\/#listItem","position":4,"name":"Sharing constants between PHP classes","previousItem":{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/programming\/#listItem","name":"Programming"}}]},{"@type":"Person","@id":"https:\/\/mamchenkov.net\/wordpress\/#person","name":"Leonid Mamchenkov","image":{"@type":"ImageObject","@id":"https:\/\/mamchenkov.net\/wordpress\/2017\/01\/30\/sharing-constants-between-php-classes\/#personImage","url":"https:\/\/secure.gravatar.com\/avatar\/3cf6df002a284d78fb6e9d8222ca4d102e0832035ed6bc8447008bd234e131a4?s=96&d=identicon&r=g","width":96,"height":96,"caption":"Leonid Mamchenkov"}},{"@type":"Person","@id":"https:\/\/mamchenkov.net\/wordpress\/author\/leonid\/#author","url":"https:\/\/mamchenkov.net\/wordpress\/author\/leonid\/","name":"Leonid Mamchenkov","image":{"@type":"ImageObject","@id":"https:\/\/mamchenkov.net\/wordpress\/2017\/01\/30\/sharing-constants-between-php-classes\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/3cf6df002a284d78fb6e9d8222ca4d102e0832035ed6bc8447008bd234e131a4?s=96&d=identicon&r=g","width":96,"height":96,"caption":"Leonid Mamchenkov"}},{"@type":"WebPage","@id":"https:\/\/mamchenkov.net\/wordpress\/2017\/01\/30\/sharing-constants-between-php-classes\/#webpage","url":"https:\/\/mamchenkov.net\/wordpress\/2017\/01\/30\/sharing-constants-between-php-classes\/","name":"Sharing constants between PHP classes - Leonid Mamchenkov","description":"When writing larger applications, it is often useful to have some constants defined, which can then be shared between different parts of the application. There are several ways to do this, and there is no real rocket science here. However, the question is: what's the best way to do so? Until now I haven't seen","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/mamchenkov.net\/wordpress\/#website"},"breadcrumb":{"@id":"https:\/\/mamchenkov.net\/wordpress\/2017\/01\/30\/sharing-constants-between-php-classes\/#breadcrumblist"},"author":{"@id":"https:\/\/mamchenkov.net\/wordpress\/author\/leonid\/#author"},"creator":{"@id":"https:\/\/mamchenkov.net\/wordpress\/author\/leonid\/#author"},"datePublished":"2017-01-30T16:10:49+02:00","dateModified":"2017-01-30T16:10:49+02:00"},{"@type":"WebSite","@id":"https:\/\/mamchenkov.net\/wordpress\/#website","url":"https:\/\/mamchenkov.net\/wordpress\/","name":"Blog of Leonid Mamchenkov","description":"Life, universe, and everything else","inLanguage":"en-US","publisher":{"@id":"https:\/\/mamchenkov.net\/wordpress\/#person"}}]},"og:locale":"en_US","og:site_name":"Leonid Mamchenkov - Life, universe, and everything else","og:type":"article","og:title":"Sharing constants between PHP classes - Leonid Mamchenkov","og:description":"When writing larger applications, it is often useful to have some constants defined, which can then be shared between different parts of the application. There are several ways to do this, and there is no real rocket science here. However, the question is: what's the best way to do so? Until now I haven't seen","og:url":"https:\/\/mamchenkov.net\/wordpress\/2017\/01\/30\/sharing-constants-between-php-classes\/","og:image":"https:\/\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2026\/03\/leonid-sailing-beer.jpg","og:image:secure_url":"https:\/\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2026\/03\/leonid-sailing-beer.jpg","og:image:width":1024,"og:image:height":1024,"article:published_time":"2017-01-30T14:10:49+00:00","article:modified_time":"2017-01-30T14:10:49+00:00","article:publisher":"https:\/\/www.facebook.com\/MamchenkovBlog","twitter:card":"summary_large_image","twitter:site":"@mamchenkov","twitter:title":"Sharing constants between PHP classes - Leonid Mamchenkov","twitter:description":"When writing larger applications, it is often useful to have some constants defined, which can then be shared between different parts of the application. There are several ways to do this, and there is no real rocket science here. However, the question is: what's the best way to do so? Until now I haven't seen","twitter:creator":"@mamchenkov","twitter:image":"https:\/\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2026\/03\/leonid-sailing-beer.jpg"},"aioseo_meta_data":{"post_id":"27279","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"BlogPosting","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2023-07-19 08:49:44","updated":"2026-01-15 12:40:45","seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/mamchenkov.net\/wordpress\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/\" title=\"Technology\">Technology<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/programming\/\" title=\"Programming\">Programming<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tSharing constants between PHP classes\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/mamchenkov.net\/wordpress"},{"label":"Technology","link":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/"},{"label":"Programming","link":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/programming\/"},{"label":"Sharing constants between PHP classes","link":"https:\/\/mamchenkov.net\/wordpress\/2017\/01\/30\/sharing-constants-between-php-classes\/"}],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":27340,"url":"https:\/\/mamchenkov.net\/wordpress\/2017\/02\/12\/php-overwrite-of-built-in-constants-true-is-false\/","url_meta":{"origin":27279,"position":0},"title":"PHP overwrite of built-in constants (true is false)","author":"Leonid Mamchenkov","date":"February 12, 2017","format":false,"excerpt":"Here is a scary thing I picked up on Reddit PHP: [code lang=\"php\"] <?php use const true as false; if (false) { echo \"uh-oh\"; } [\/code] Until PHP 5.6 this was throwing a parse error, but from then on - it's just fine. \u00a0Scary, right? The comments on the Reddit\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":29105,"url":"https:\/\/mamchenkov.net\/wordpress\/2018\/12\/17\/the-best-way-to-get-the-full-php-version-string\/","url_meta":{"origin":27279,"position":1},"title":"The best way to get the full PHP version string","author":"Leonid Mamchenkov","date":"December 17, 2018","format":false,"excerpt":"Jeff Geerling shares the best way to get the full PHP version string.\u00a0 I'd think that \"php --version\" externally or \"echo PHP_VERSION\" internally would do the job.\u00a0 However, that's not exactly right, as there are a number of inconsistencies on different platforms.\u00a0 The best option seems to be the combination\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26776,"url":"https:\/\/mamchenkov.net\/wordpress\/2016\/10\/23\/tagbar-phpctags-vim-plugin-for-php-developeres\/","url_meta":{"origin":27279,"position":2},"title":"tagbar-phpctags : Vim plugin for PHP developeres","author":"Leonid Mamchenkov","date":"October 23, 2016","format":false,"excerpt":"If you are using Vim editor to write PHP code, you probably already know about the excellent tagbar plugin, which lists methods, variables and the like in an optional window split. \u00a0Recently, I've learned of an awesome phpctags-tagbar plugin, which extends and improves this functionality via a phpctags tool, which\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"phpctags","src":"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2016\/10\/phpctags-500x270.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":26996,"url":"https:\/\/mamchenkov.net\/wordpress\/2016\/11\/28\/runkit-changing-the-php-internals-on-the-fly\/","url_meta":{"origin":27279,"position":3},"title":"runkit &#8211; changing the PHP internals on the fly","author":"Leonid Mamchenkov","date":"November 28, 2016","format":false,"excerpt":"Here is something I didn't know about until today - PHP's runkit extension: The runkit extension provides means to modify constants, user-defined functions, and user-defined classes. It also provides for custom superglobal variables and embeddable sub-interpreters via sandboxing. This blog post - \"Shimming PHP for Fun and Profit\" - demonstrates\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":16045,"url":"https:\/\/mamchenkov.net\/wordpress\/2011\/12\/29\/day-in-brief-2011-12-29\/","url_meta":{"origin":27279,"position":4},"title":"Day in brief &#8211; 2011-12-29","author":"Leonid Mamchenkov","date":"December 29, 2011","format":false,"excerpt":"I favorited a @YouTube video http:\/\/t.co\/G67ys1ZL \u0412 \u0420\u043e\u0441\u0441\u0438\u0438 \u0441\u0430\u043c\u044b\u0435 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0435 \u0434\u043e\u0440\u043e\u0433\u0438 # 69% of Cyprus online users are on Facebook. Are they really? http:\/\/t.co\/3Sek36oF # New note : Dan Ariely \u00bb Blog Archive This Is How I Feel About Buying Apps \u00ab http:\/\/t.co\/DTo4Yf1n # New note : WordPress constants overview\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":16526,"url":"https:\/\/mamchenkov.net\/wordpress\/2012\/07\/20\/wp-config-php-and-git\/","url_meta":{"origin":27279,"position":5},"title":"wp-config.php and git","author":"Leonid Mamchenkov","date":"July 20, 2012","format":false,"excerpt":"If you are storing your WordPress changes in git and then deploy the project between different machines (local, test server, production environment, etc), then you are probably familiar with a problem of wp-config.php file. \u00a0WordPress uses it for things like database credentials, which vary from machine to machine. \u00a0But you\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/27279","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/comments?post=27279"}],"version-history":[{"count":0,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/27279\/revisions"}],"wp:attachment":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/media?parent=27279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/categories?post=27279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/tags?post=27279"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/keyring_services?post=27279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}