{"id":280435,"date":"2021-02-03T03:46:06","date_gmt":"2021-02-03T03:46:06","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=280435"},"modified":"2025-12-17T12:20:11","modified_gmt":"2025-12-17T12:20:11","slug":"static-function-call-with-static-keyword-in-php","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/static-function-call-with-static-keyword-in-php\/","title":{"rendered":"Overriding\/Static function call with static Keyword in PHP"},"content":{"rendered":"\n<p>We will explore here how we can call overridden static functions from the base class If that function is overridden by the derived class.<\/p>\n\n\n\n<p>Lets understand it with an example of problem and solution code &#8211;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">What&#8217;s The Problem ?<\/h4>\n\n\n\n<p>Suppose you create two static methods in a class <strong>BaseClass<\/strong>.<br>You have called the first static method getStaticText() from the second static method getStaticResult().<\/p>\n\n\n\n<p>Now a child class <strong>DerivedClass<\/strong> extends the <strong>BaseClass<\/strong> and override the static method <strong>getStaticText()<\/strong>.<\/p>\n\n\n\n<p>See the below piece of code to understand it properly &#8211;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?php\n\tClass BaseClass \n\t{\n\t    \/\/ Static functions\n\t    public static function getStaticText()\n\t    {\n\t        return 'Called from BaseClass';\n\t    }\n\n\t    public static function getStaticResult()\n\t    {\n\t        \/\/ we can alternatively use BaseClass::getStaticText();\n\t        return self::getStaticText();\n\t    }\n\t}\n\n\tClass DerivedClass extends BaseClass\n\t{\n\t    public static function getStaticText()\n\t    {\t\n\t        return 'Called from DerivedClass';\n\t    }\n\t}\n\n\tvar_dump(DerivedClass::getStaticResult());\n\n?&gt;\n<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>Now if you call <strong>getStaticResult()<\/strong> from <strong>DerivedClass <\/strong> <strong>&#8211;<\/strong><\/p>\n\n\n\n<p>then the result of <strong>var_dump(DerivedClass::getStaticResult())<\/strong> will be :<\/p>\n\n\n\n<p> &#8220;<strong>Called from BaseClass<\/strong>&#8220;.<\/p>\n\n\n\n<p>The getStaticText() function called from BaseClass as it is called with self keyword.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Case :<\/h4>\n\n\n\n<p>You have overrided the getStaticText() method in the DerivedClass.<\/p>\n\n\n\n<p>Also you have called the method from DerivedClass  &#8211; var_dump(DerivedClass::getStaticResult());<\/p>\n\n\n\n<p><strong>Here Let&#8217;s suppose<\/strong> <strong><em>you want,  if any method is overridden in the child class then it must be called from the child class. <\/em><\/strong>If calling class is child class (DerivedClass).<\/p>\n\n\n\n<p><strong><em>In the above code I want getStaticText()<\/em><\/strong> <strong><em>must be called from DerivedClass.<\/em><\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You may say call <strong>DerivedClass::getStaticText();<\/strong> in place of <strong>self::getStaticText();<\/strong> in <strong><strong>BaseClass<\/strong><\/strong>. But how someone knows while creating <strong>BaseClass <\/strong>that which class is going to extend BaseClass.<\/li>\n<\/ul>\n\n\n\n<p>Then how to achieve this generically?<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Solution \ud83d\ude42 :<\/h4>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Here comes the call of static method with static keyword.<\/strong><\/p>\n\n\n\n<p>If you use static keyword to call the static functions in the class then &#8211;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In case there is no overridden function then it will call the function within the class as self keyword does.<\/li>\n\n\n\n<li>If the function is overridden then the static keyword will call the overridden function in the derived class.<\/li>\n<\/ul>\n\n\n\n<p>In the below code we have used <strong>static::getStaticText()<\/strong> in place of <strong><em>self::getStaticText()<\/em><\/strong> in the <strong>getStaticResult()<\/strong> method of <strong>BaseClass<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?php\n\n\tClass BaseClass \n\t{\n\t    \/\/ Static functions\n\t    public static function getStaticText()\n\t    {\n\t        return 'Called from BaseClass';\n\t    }\n\n\t    public static function getStaticResult()\n\t    {\n\t        \/\/ we can alternatively use BaseClass::getStaticText();\n\t        return static::getStaticText();\n\t    }\n\t}\n\n\tClass DerivedClass extends BaseClass\n\t{\n\t    public static function getStaticText()\n\t    {\t\n\t        return 'Called from DerivedClass';\n\t    }\n\t}\n\n\tvar_dump(DerivedClass::getStaticResult());\n\n?&gt;\n<\/pre>\n\n\n\n<p>Now, the result of <strong>var_dump(DerivedClass::getStaticResult())<\/strong> will be :<\/p>\n\n\n\n<p> &#8220;<strong>Called from DerivedClass<\/strong>&#8220;.<\/p>\n\n\n\n<p>So the overridden getStaticText() function called this time. This is how static function call with static function works. To know more usees of static visit <strong><a href=\"https:\/\/www.php.net\/manual\/en\/language.oop5.static.php\">Static Keyword<\/a><\/strong>.<\/p>\n\n\n\n<p>Hope you have understood the concept and use of static method for overrided static methods and it will help you somewhere in development.<\/p>\n\n\n\n<p>Happy Coding! \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We will explore here how we can call overridden static functions from the base class If that function is overridden by the derived class. Lets understand it with an example of problem and solution code &#8211; What&#8217;s The Problem ? Suppose you create two static methods in a class BaseClass.You have called the first static <a href=\"https:\/\/webkul.com\/blog\/static-function-call-with-static-keyword-in-php\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":83,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[9348],"class_list":["post-280435","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-static-block"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Overriding\/Static function call with static Keyword in PHP<\/title>\n<meta name=\"description\" content=\"A static method call with static Keyword. this keyword will call the overridden function in the derived class.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/static-function-call-with-static-keyword-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Overriding\/Static function call with static Keyword in PHP\" \/>\n<meta property=\"og:description\" content=\"A static method call with static Keyword. this keyword will call the overridden function in the derived class.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/static-function-call-with-static-keyword-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-02-03T03:46:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-12-17T12:20:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Sumit\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sumit\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/static-function-call-with-static-keyword-in-php\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/static-function-call-with-static-keyword-in-php\/\"},\"author\":{\"name\":\"Sumit\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/3e45ec35749afa62aa598a5e1766d2b9\"},\"headline\":\"Overriding\/Static function call with static Keyword in PHP\",\"datePublished\":\"2021-02-03T03:46:06+00:00\",\"dateModified\":\"2025-12-17T12:20:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/static-function-call-with-static-keyword-in-php\/\"},\"wordCount\":379,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"static block\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/static-function-call-with-static-keyword-in-php\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/static-function-call-with-static-keyword-in-php\/\",\"url\":\"https:\/\/webkul.com\/blog\/static-function-call-with-static-keyword-in-php\/\",\"name\":\"Overriding\/Static function call with static Keyword in PHP\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2021-02-03T03:46:06+00:00\",\"dateModified\":\"2025-12-17T12:20:11+00:00\",\"description\":\"A static method call with static Keyword. this keyword will call the overridden function in the derived class.\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/static-function-call-with-static-keyword-in-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/static-function-call-with-static-keyword-in-php\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/static-function-call-with-static-keyword-in-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Overriding\/Static function call with static Keyword in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/3e45ec35749afa62aa598a5e1766d2b9\",\"name\":\"Sumit\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0e50336dc34ad31135238f210897d19d09edbdb9be2f7974a85de3ecdef16bf6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0e50336dc34ad31135238f210897d19d09edbdb9be2f7974a85de3ecdef16bf6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Sumit\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/sumit201\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Overriding\/Static function call with static Keyword in PHP","description":"A static method call with static Keyword. this keyword will call the overridden function in the derived class.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/static-function-call-with-static-keyword-in-php\/","og_locale":"en_US","og_type":"article","og_title":"Overriding\/Static function call with static Keyword in PHP","og_description":"A static method call with static Keyword. this keyword will call the overridden function in the derived class.","og_url":"https:\/\/webkul.com\/blog\/static-function-call-with-static-keyword-in-php\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2021-02-03T03:46:06+00:00","article_modified_time":"2025-12-17T12:20:11+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Sumit","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Sumit","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/static-function-call-with-static-keyword-in-php\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/static-function-call-with-static-keyword-in-php\/"},"author":{"name":"Sumit","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/3e45ec35749afa62aa598a5e1766d2b9"},"headline":"Overriding\/Static function call with static Keyword in PHP","datePublished":"2021-02-03T03:46:06+00:00","dateModified":"2025-12-17T12:20:11+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/static-function-call-with-static-keyword-in-php\/"},"wordCount":379,"commentCount":1,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["static block"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/static-function-call-with-static-keyword-in-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/static-function-call-with-static-keyword-in-php\/","url":"https:\/\/webkul.com\/blog\/static-function-call-with-static-keyword-in-php\/","name":"Overriding\/Static function call with static Keyword in PHP","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2021-02-03T03:46:06+00:00","dateModified":"2025-12-17T12:20:11+00:00","description":"A static method call with static Keyword. this keyword will call the overridden function in the derived class.","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/static-function-call-with-static-keyword-in-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/static-function-call-with-static-keyword-in-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/static-function-call-with-static-keyword-in-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Overriding\/Static function call with static Keyword in PHP"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/3e45ec35749afa62aa598a5e1766d2b9","name":"Sumit","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/0e50336dc34ad31135238f210897d19d09edbdb9be2f7974a85de3ecdef16bf6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0e50336dc34ad31135238f210897d19d09edbdb9be2f7974a85de3ecdef16bf6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Sumit"},"url":"https:\/\/webkul.com\/blog\/author\/sumit201\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/280435","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/83"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=280435"}],"version-history":[{"count":53,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/280435\/revisions"}],"predecessor-version":[{"id":517801,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/280435\/revisions\/517801"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=280435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=280435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=280435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}