skip to Main Content

I would like to believe that I understand the concept of static/early binding vs late/dynamic binding as well as late static binding but I found some conflicting definitions after reading few articles about it.

From my understanding the following:

<?php
    class A {
       protected $greeting = 'Hello from A';

       public function hello() {
           echo $this->greeting;
       }
    }

    $obj = new A();
    $obj->hello();

is an example of static or early binding which happens at compile time (even though PHP is interpreted language). It is early binding because all information about the class is known & nothing needs to be dynamically figured out, so class & method binding happens at compile time.

Dynamic binding or also known as late binding happens at runtime where class & method binding happens at runtime. If we take the same example as above but use something like inheritance, that would be late or dynamic binding:

 <?php
    class B extends A {           
       protected $greeting = 'Hello from B';
    }

    $obj = new B();
    $obj->hello();

so my question is, is my assumption/understanding of static/dynamic binding correct in PHP? I know there is also late static binding which combines static & late binding and makes static properties & methods work with inheritance, so instead of using self:: you would use static:: which would wait for runtime to do the binding.

For reference, these are the articles I’ve read after having doubts about my own understanding of this concept:

This article states that

Static binding happens when you use the scope resolution operator ::.

but per my understanding that is not always the case, isn’t my example above the one without inheritance a version of static binding as well? Static in terms of binding does not necessarily mean static variables.

The following articles are the ones with conflicting information, so this is why I am kind of confused & want to know whether I understand it correctly & if my examples are correct or not. I could not find the original RFC on PHP to get more insight as to how exactly this works.

https://www.codeproject.com/Articles/853792/A-Walk-Through-Into-Late-Static-Binding-in-PHP

https://joshduck.com/blog/2010/03/19/exploring-phps-static-scoping/

https://blog.julien-maury.dev/en/php-late-static-binding/

Which one of these articles is more on point as to how binding really works in PHP?

2

Answers


  1. yes your understanding is correct, because after creating class B and extending it from A, there two different versions of function hello(), which one is called at runtime depends on the type of object(A or B – to be determined by the context) calling it. Another way to look at it is that it is polymorphism.

    Login or Signup to reply.
  2. … is an example of static or early binding which happens at compile time (even though PHP is interpreted language).

    You’ve managed to fit quite a lot of confusion into one sentence here.

    Firstly, PHP is a compiled language, it is just compiled "on demand", into a high-level intermediate representation. What’s more relevant is that PHP is a highly dynamic language, which doesn’t do very much analysis during compilation.

    Secondly, the example code you show could be optimised to a known method at compile-time, but it might not be. As far as the language is concerned, the statement $obj->hello(); is evaluated at run-time based on the current value of $obj. The fact that you can see the class to use on the line above, and know that that class has no parents, doesn’t mean the compiler will definitely know those things and compile the code differently.

    Thirdly, "early" and "static" can’t just be used as synonyms – otherwise, the term "late static binding" would mean "late early binding", and make no sense. A "static" call is one that references a particular class; a "non-static" call is one which references an instance.

    The important difference from the user’s point of view is that given these three lines of code:

    $someObject->methodOne();
    self::methodTwo();
    static::methodThree();
    
    • The definition of methodOne used will be whichever class $someObject is an instance of at the time that code runs. It might be a different method each time the line of code runs, if $someObject has a different value. This is late binding.
    • It will also reference the specific instance in $someObject, placing it in the magic variable $this. It is a non-static call.
    • The definition of methodTwo used will be the one in the class where that line of code is written. Every time that line of code runs, it will reference the same class. This is early binding.
    • The definition of methodThree used will depend on how the code was called – if it was called with the name of a child class with its own version of methodThree, that version will be used. Like methodOne, the line might run a different method each time. This is late binding.
    • Both methodTwo and methodThree will only reference a class, not an instance. They will not populate the magic variable $this. They are static calls.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search