oop - Is there any reason to use self:: over static:: when calling static method inside a PHP class

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 usestatic::
then as well?
Another way to ask the same, what should be the rule of thumb when calling static methods, to usestatic::
orself::
, or is there such a big drawback for usingstatic::
that you should use it only when strickly required?
Answer
Solution:
There's no difference between them in a final class, so use whichever you want.
Both calls will returnA
.
<?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);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: the process class relies on proc_open, which is not available on your php installation.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.