-1

What happens when we calling non-static functions with static way?
Like this :

class test
{
    public function hello()
    {
        return "say hello";
    }
}

class foo
{
    public function __construct()
    {
        echo test::hello();
    }
}

$foo = new foo();
Ali
  • 443
  • 5
  • 22
  • what is that parenthesis on the class name declaration? most likely an error, try it – Kevin Jan 30 '15 at 09:35
  • `Strict standards: Non-static method test::hello() should not be called statically, assuming $this from incompatible context`, you're welcome! You can download PHP from here so you can try yourself rather than having to ask the outcome of every piece of code, as that's probably quite a slow way to develop! http://php.net/downloads.php – SubjectCurio Jan 30 '15 at 09:36
  • i fixed issue, sorry – Ali Jan 30 '15 at 09:37

2 Answers2

1

You will get Strict standards: Non-static method test::hello() should not be called statically

and your Class declaration is not good, right way is this

class test
{
    public function hello()
    {
        return "say hello";
    }
}

class foo
{
    public function __construct()
    {
        echo test::hello();
    }
}

$foo = new foo();

Laravel uses Facade to give illusion that you are using static method, you can also use Facade to call method like test::hello();

Here is a good read on Facade

Saqueib
  • 3,484
  • 3
  • 33
  • 56
0

Fast answer: You get a strict warning.

More details: If the variable $this is used in the function called it refers to the object where the function got called, not the class containing the function itself.

Strict standards: Non-static method hello should not be called statically, assuming $this from incompatible context

Salketer
  • 14,263
  • 2
  • 30
  • 58
  • What happens when we receive strict warning? It will be cause performance issue or other issue? – Ali Jan 30 '15 at 09:47
  • I do not know about performance issues I haven't tested, but I don't think so. I am actually using this flawed design to be able to stay DRY on a bad code usage... Works perfect for me once notices are turned off. I do know that we have over commented the function to be sure... – Salketer Jan 30 '15 at 09:49