I have a code in php,
<?php
class DB{
protected static $table = 'basetable';
public function Select(){
echo "Select * from" . static::$table;
}
}
class Abc extends DB{
protected static $table = 'abc';
}
$abc = new Abc();
$abc->Select();
from my understanding of late static binding what the above program will return is as follows,
Select * from abc;
and if we dont use late static binding above program will return,
Select * from basetable
because of late static binding the value of $table is substituted in runtime instead of compile time.
But doesn't inheritance give the same output?
since as per the laws of inheritance, the parent attribute is overridden by child attributes
and since $table = abc in child(class Abc), won't it be overridden on $table in parent(Class DB)?
staticvalues are looked up differently than$thisvariables/properties. In the compiler, they're bound early when you useself:This will always output
baz, regardless of whether youextendthe class with something else or not. That's where you need late static binding withstaticinstead ofself.Essentially it enables inheritance for
staticvalues, which wasn't possible before it existed.