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.

7
  • Running it here: 3v4l.org/rfQSe shown 100... Commented Feb 19, 2016 at 14:39
  • yes actually I have edited the function name now. it is now sum() Commented Feb 19, 2016 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. Commented Feb 19, 2016 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 Commented Feb 19, 2016 at 14:42
  • stackoverflow.com/questions/3754786/… Commented Feb 19, 2016 at 14:45

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I was just checking oops concept. It is not related to my any project.
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();

3 Comments

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.
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.
It also makes a difference whether you call a static method outside of or inside of a class.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.