I am just checking the OOPs static/non-static concept and found something strange.
I have heard that static method's output can be get by using static keyword with resolution operator(::) . But In my program I am getting non static method's value using static keyword. Can any one explain the program? I am getting confused.
<?php
error_reporting(E_ALL);
class parentclass
{
protected function sum()
{
return 145;
}
}
class childclass extends parentclass
{
protected function sum()
{
return 125;
}
}
class grandchild extends childclass
{
function sum()
{
return 100;
}
function __construct()
{
echo static::sum(); // 100 as output but how
}
}
$obj = new grandchild();
?>
Besides this If I am making function sum() of childclass as static like
class childclass extends parentclass
{
protected static function sum()
{
return 125;
}
}
then also it is giving fatal error as following:
Fatal error: Cannot make non static method parentclass::sum() static in class childclass
But why I am not calling that function.