1

static:: provides late binding which is a must if the static functions of the class can be overriden by an extending class and the static method is called from within the class. But in the case that a class can not be extended (it is final for example) would it be wrong to use static:: then as well?

Another way to ask the same, what should be the rule of thumb when calling static methods, to use static:: or self::, or is there such a big drawback for using static:: that you should use it only when strickly required?

Mark Kaplun
  • 276
  • 2
  • 13
  • I know it might be an opinion thing or relating to coding standards for a specific project, the question is an attempt to learn if there is some kind of a concensus about what should be the rule of thumb. – Mark Kaplun Jul 29 '21 at 14:23
  • https://stackoverflow.com/questions/5197300/new-self-vs-new-static explains the differences – aynber Jul 29 '21 at 14:26
  • "rules of thumb" are a bad idea and encourage blind adherence to one type of procedure, when there may be different options available. Instead, read about the differences between each one, and decide which one is most suitable in each scenario you encounter. If you're unsure, set up a test scenario to see if anything goes wrong with one or the other in a particular situation. – ADyson Jul 29 '21 at 14:27
  • @aynber, I didn't ask about the difference, I ask about situations in which there are no execution paths differences like in a final class or final methods – Mark Kaplun Jul 29 '21 at 14:34

1 Answers1

1

There's no difference between them in a final class, so use whichever you want.

Both calls will return A.

<?php

class Dad {
    static function getStatic() {
        return new static;
    }
    
    static function getSelf() {
        return new self;
    }
}

trait Useless {
    static function getStatic() {
        return new static;
    }
}

final class A extends Dad {
    use Useless;
    
    static function getSelf() {
        return new self;
    }
}

var_dump(A::getStatic()::class);
var_dump(A::getSelf()::class);

Example

Dharman
  • 30,962
  • 25
  • 85
  • 135