In first case we called function in object context. In second case we have class context.
Does parent:: work like this and self simultaneously depending on the context?
class par_parent{
private $var='value1';
private static $val='value2';
public function call(){
var_dump('Object '.$this->var);
}
public static function staticCall(){
var_dump('Static '.self::$val);
}
}
class par_child extends par_parent{
public function callObj(){
parent::call();
}
public static function callStatic(){
parent::staticCall();
}
}
$obj=new par_child();
$obj->callObj();
**//string 'Object value1' (length=13)**
par_child::callStatic();
**//string 'Static value2' (length=13)**
The
parent::is binded like theself::keyword, always sees the context where it have been defined code wise, not from where it’s called, so in essence it works like theself::keyword. If you need it to work like the$thisuse late static binding proviededstatic::. Consider this example:Here we call the
say()method onCit comes from the classBso it’sparent::meansAand theA::$vis ‘a’ so it prints that.While the
parent::inCpoints to the classBand it sees it’s$vas ‘b’.With php 5.3 comes late static binding and the
static::keyword that lets you access thestaticvariables and methods in baseclasses static methods so theA::staticsaywill see the$vfrom the classC.