Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7941131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:34:34+00:00 2026-06-03T23:34:34+00:00

I’m having difficulty understanding how ‘static’ stuff works in PHP. Can someone point me

  • 0

I’m having difficulty understanding how ‘static’ stuff works in PHP. Can someone point me to a good tutorial on this? One that comprehensively covers the topic?

I understand the use of static variables within a function, and also static members in a class. Beyond that, however, it gets a bit hazy.

For example: I see that static methods can be called without instantiating a class. Not sure why this is important though, since you can call a non-static method in exactly the same way, provided it does not reference $this

Late static binding is another area of confusion, along with when and where I can use the scope resolution operator. (It seems I can use it to call parent methods irrespective of whether they are static or not…)

Am I the only one struggling with this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:34:36+00:00Added an answer on June 3, 2026 at 11:34 pm

    While you can call a non-static method without an instance, this will trigger an E_STRICT warning. The whole point of static methods is for accessing static variables on the class, but many people use it as a way to logically group utility functions rather than define a bunch of global functions.

    When you call a static method using the class’s name, e.g. Class::foo(), there is no instance and thus no polymorphism. The foo defined by Class is called directly. If it doesn’t define such a method, its superclasses are searched until one is found that does.

    class Parent {
        static function foo() {
            echo "Parent::foo";
        }
        static function bar() {
            echo "Parent::bar";
        }
    }
    
    class Child extends Parent {
        static function foo() {
            echo "Child::foo";
        }
    }
    
    Parent::foo();   // Parent::foo
    Parent::bar();   // Parent::bar
    
    Child::foo();    // Child::foo
    Child::bar();    // Parent::bar
    

    When you call a static method using the self keyword from a class method, e.g. self::foo(), it works just as if you were to replace self with the name of the class holding the calling code.

    class Parent {
        static function foo() {
            echo "Parent::foo";
        }
        static function callFoo() {
            self::foo();    // equivalent to Parent::foo()
        }
    }
    
    class Child extends Parent {
        static function foo() {
            echo "Child::foo";
        }
    }
    
    Parent::callFoo();   // Parent::foo
    Child::callFoo();    // Parent::foo
    

    When you call a static method using the static keyword from a class method, e.g. static::foo(), you are invoking late static binding. Instead of starting the search for foo in the current class, it starts from the current class context, the class that was initially statically-referenced.

    class Parent {
        static function foo() {
            echo "Parent::foo";
        }
        static function callFoo() {
            static::foo();         // late static binding
        }
    }
    
    class Child extends Parent {
        static function foo() {
            echo "Child::foo";
        }
        static function callParentCallFoo() {
            Parent::callFoo();    // resets static context to Parent
        }
    }
    
    Parent::callFoo();             // Parent::foo
    Child::callFoo();              // Child::foo
    Child::callParentCallFoo();    // Parent::foo
    

    Late static binding works similarly with static class properties, but the property must be defined in the child class itself. Assigning a new property to a class (e.g. Child::$foo = 'foo') will not make it available for LSB from the parent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.