0

I am doing a PHP tutorial and I found this code

Class Insurance
{
   function clsName()
   {
      echo get_class($this)."\n";
   }
}


$cl = new Insurance();
$cl->clsName();
Insurance::clsName();

here function clsName() is accessed without creating an instance of Insuarance

Insurance::clsName();

But from the definition

The Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden properties or methods of a class.

When referencing these items from outside the class definition, use the name of the class.

http://php.net/manual/en/language.oop5.paamayim-nekudotayim.php

I searched in web but coul not find a good explanation why this code is working? please explain.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193
  • Because that is how php should behave ? That's the point of a static method/property. I don't understand what you want now? – Rizier123 Feb 12 '15 at 16:11
  • 1
    @Rizier123 function clsName() is not a static method – Kanishka Panamaldeniya Feb 12 '15 at 16:11
  • 1
    This shouldn't work though, because the `clsName()` method contains an explicit reference to `$this`, which doesn't exist in a static context – Mark Baker Feb 12 '15 at 16:12
  • 1
    Error reporting: `` == `Strict Standards: Non-static method Insurance::clsName() should not be called statically in C:\xampp\htdocs\Sandbox\index.php on line 30 Notice: Undefined variable: this in C:\xampp\htdocs\Sandbox\index.php on line 23` – Rizier123 Feb 12 '15 at 16:13
  • @Rizier123 It's not a static method. This is a legit question. – Brian Warshaw Feb 12 '15 at 16:13
  • I checked the code in http://writecodeonline.com/php/ – Kanishka Panamaldeniya Feb 12 '15 at 16:13
  • 2
    Static access to non-static methods used to work without problem (except when the methods referenced $this) but that has been tightened up in more recent versions of PHP – Mark Baker Feb 12 '15 at 16:13
  • http://3v4l.org/TTd30 – Mark Baker Feb 12 '15 at 16:14
  • @KanishkaPanamaldeniya You get almost in every version a php warning/error: http://3v4l.org/F1u1q – Rizier123 Feb 12 '15 at 16:14
  • 2
    for all the fancy stuff around them, methods are still just functions, and you can call them however/wherever you want. But that doesn't mean they'll work CORRECTLY. e.g. calling your method will produce errors because `$this` is not available when called statically. – Marc B Feb 12 '15 at 16:15
  • http://stackoverflow.com/questions/3754786/calling-non-static-method-with – Kostia Shiian Feb 12 '15 at 16:15

1 Answers1

1

When I run it with error reporting E_ALL :

Insurance
<br />
<b>Strict Standards</b>:  Non-static method Insurance::clsName() should not be called statically in <b>[...][...]</b> on line <b>12</b><br />
<br />
<b>Notice</b>:  Undefined variable: this in <b>[...][...]</b> on line <b>5</b><br />
Insurance

Now the question is why it still working ? as you can see, "Insurance" was displayed.

When you do echo get_class($this)."\n"; when you call in a static context, PHP will run it like echo get_class(null)."\n";.

And if you read the behavior of get_class http://php.net/manual/en/function.get-class.php, The class is recognized because the function was call inside the class.

Bang
  • 919
  • 4
  • 11