I came across some code today that uses PHP's late static binding and the way it's used doesn't make sense to me. Late static binding is a new concept for me so I'm trying to wrap my head around it.
After studying this Github Gist and reading this Stack Overflow post, I understand what late static binding does and why it's used. Now I'm trying to apply that to the code I'm looking at and it doesn't make sense to me. It feels like the static
keyword is being used incorrectly. Here's more or less the code I'm not sure about:
<?php
class myClass
{
const MY_CONST = 'some string';
public function getMyConst()
{
return static::MY_CONST;
}
}
$c = new myClass();
print_r($c->getMyConst());
Assuming I'm understanding the concept, late static binding let's you reference a child's const in a static context. So in the code I'm looking at there's no inheritance and there's no static function, so what is the point of using the static
keyword vs the self
keyword in this particular use case? Also, does it hurt anything to use the static
keyword in this way, assuming that it's being used incorrectly (or maybe not exactly as it was intended to be used)?