0

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.

Deep Kakkar
  • 5,831
  • 4
  • 39
  • 75
  • Running it here: https://3v4l.org/rfQSe shown 100... – Alex Tartan Feb 19 '16 at 14:39
  • yes actually I have edited the function name now. it is now sum() – Deep Kakkar Feb 19 '16 at 14:41
  • If you are not absolutely sure when statics are useful, try to avoid them. In your example, it does not make sense to use static. – Daniel W. Feb 19 '16 at 14:41
  • I am just trying to get the knowledge and the cause why it is giving output as 100. I mean sum() is non static and I am calling using static:: resolution operator. If you know the reason then please let me know @DanFromGermany – Deep Kakkar Feb 19 '16 at 14:42
  • http://stackoverflow.com/questions/3754786/calling-non-static-method-with – DarkBee Feb 19 '16 at 14:45
  • @DarkBee I have edited my question. there is not only question of static and non static.. I am getting fatal error If I am making parent class function as static. – Deep Kakkar Feb 19 '16 at 14:56
  • What are you expecting? `100`? `125`? `145`? – h2ooooooo Feb 19 '16 at 15:02

3 Answers3

1

You are using static as a Late Static Binding. But what you heard about was rather

class Foo
{
  static function bar() {}
}

$baz = Foo::bar();
Maru
  • 894
  • 1
  • 12
  • 29
  • yeah, just follow the link and read the article on it. You might then understand how `self` and `static` work and maybe even why they are different. – Maru Feb 19 '16 at 14:49
  • Yes dear I know the difference between self and static keywords, I know the concept of late static binding. static keyword is used for late static binding but here is different concept. – Deep Kakkar Feb 19 '16 at 14:54
  • It also makes a difference whether you call a static method outside of or inside of a class. – Daniel W. Feb 19 '16 at 14:57
1

You can call a function statically, even if it is not declared as static, as long as you don't reference $this inside it.

That is why the original code works.

You cannot, however, change the signature of an inherited method.

Fatal error: Cannot make non static method parentclass::sum() static in class childclass

When you declare protected static function sum() in childclass, you are changing the signature of the inherited method from parentclass which is not specifically declared static.

Bottomline, you are trying to use some PHP quirks that I would recommend against. Yes, they work, but that doesn't mean you should use them.

Stick to a strict style of coding. Write separate methods for static and instance use and call them as intended.

Mihai Răducanu
  • 12,225
  • 4
  • 22
  • 31
0

I think to understand late static bindings a bit better you could write a variation of your code:

<?php
error_reporting(E_ALL);

class parentclass
{
    protected function sum()
    {
        return 145;
    }

    public function do_the_math()
    {
        printf('Static sum: %d, Self sum: %d', 
            static::sum(), 
            self::sum());
    }
}

class childclass extends parentclass
{
    protected function sum()
    {
        return 125;
    }
}

class grandchild extends childclass
{
    function sum()
    {
        return 100;
    }
}

$obj = new grandchild(); 
$obj->do_the_math();

Output:

Static sum: 100, Self sum: 145
Progrock
  • 7,373
  • 1
  • 19
  • 25