{"id":820,"date":"2018-11-15T03:58:21","date_gmt":"2018-11-15T03:58:21","guid":{"rendered":"https:\/\/barkhane.com\/php\/2018\/11\/15\/static-keyword-in-php\/"},"modified":"2024-03-03T14:32:06","modified_gmt":"2024-03-03T14:32:06","slug":"static-keyword-in-php","status":"publish","type":"post","link":"https:\/\/barkhane.com\/php\/static-keyword-in-php\/","title":{"rendered":"Static Keyword in PHP"},"content":{"rendered":"<p>Static Keyword in PHP :<\/p>\n<p>Declaring class members or methods as static makes them accessible without needing an instantiation of the class. A member declared as static can not be accessed with an instantiated <a href=\"https:\/\/barkhane.com\/php\/classes-in-php\/\">class<\/a> object (though a static method can).<\/p>\n<p>Try out following example \u2212<\/p>\n<pre class=\"prettyprint notranslate prettyprinted\"><span class=\"pun\">&lt;?<\/span><span class=\"pln\">php\n   <\/span><span class=\"kwd\">class<\/span> <span class=\"typ\">Foo<\/span> <span class=\"pun\">{<\/span>\n      <span class=\"kwd\">public<\/span> <span class=\"kwd\">static<\/span><span class=\"pln\"> $my_static <\/span><span class=\"pun\">=<\/span> <span class=\"str\">\\'foo\\'<\/span><span class=\"pun\">;<\/span>\n      \n      <span class=\"kwd\">public<\/span> <span class=\"kwd\">function<\/span><span class=\"pln\"> staticValue<\/span><span class=\"pun\">()<\/span> <span class=\"pun\">{<\/span>\n         <span class=\"kwd\">return<\/span> <span class=\"kwd\">self<\/span><span class=\"pun\">::<\/span><span class=\"pln\">$my_static<\/span><span class=\"pun\">;<\/span>\n      <span class=\"pun\">}<\/span>\n   <span class=\"pun\">}<\/span>\n\t\n   <span class=\"kwd\">print<\/span> <span class=\"typ\">Foo<\/span><span class=\"pun\">::<\/span><span class=\"pln\">$my_static <\/span><span class=\"pun\">.<\/span> <span class=\"str\">\\\"\\\\n\\\"<\/span><span class=\"pun\">;<\/span><span class=\"pln\">\n   $foo <\/span><span class=\"pun\">=<\/span> <span class=\"kwd\">new<\/span> <span class=\"typ\">Foo<\/span><span class=\"pun\">();<\/span>\n   \n   <span class=\"kwd\">print<\/span><span class=\"pln\"> $foo<\/span><span class=\"pun\">-&gt;<\/span><span class=\"pln\">staticValue<\/span><span class=\"pun\">()<\/span> <span class=\"pun\">.<\/span> <span class=\"str\">\\\"\\\\n\\\"<\/span><span class=\"pun\">;<\/span>\n<span class=\"pun\">?&gt;<\/span><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Static Keyword in PHP : Declaring class members or methods as static makes them accessible without needing an instantiation of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[3],"tags":[],"class_list":["post-820","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts\/820","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/comments?post=820"}],"version-history":[{"count":0,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts\/820\/revisions"}],"wp:attachment":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/media?parent=820"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/categories?post=820"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/tags?post=820"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}