Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php static methods question

Tags:

oop

php

What is the difference between these two pieces of code?

class something {

   static function doit() {
      echo 'hello world';
   }
}

something::doit();

and the same but without the static keyword

class something {

   function doit() {
      echo 'hello world';
   }
}

something::doit();

They both work the same is it better to use the static keywords? Am i right in understanding that it doesn't instantiate the class if you use the static method?

like image 446
geoffs3310 Avatar asked Feb 03 '11 09:02

geoffs3310


1 Answers

The second example is technically incorrect - if you turn on E_STRICT error reporting you'll see that PHP is actually throwing an error.

PHP Strict Standards: Non-static method something::doit() should not be called statically in...

In other words, it's being nice and letting you call the function anyway.

like image 105
John Parker Avatar answered Oct 04 '22 06:10

John Parker