Hey folks,

I've found 4 ways to call static methods and I'm confued about them.
The ways are:

1- className::function()
2- $this->function()
3- self::function()
4- static::function()

1-What are the differences between all of them?
2-What's the main reason for using static methods against non-static methods?

Pretty much there is not much difference except you cannot use Scope Resolution Operator :: for non-static methods AND static method does not need an instance of the object.



is use outside the class, while these


are use within other methods within the class. This


if use in other static method will throw not in object context error.

Static method is commonly used in Singleton pattern, database connection or anything that don't need to instantiate the entire class. In short static method is independent to the class or object where it belongs.

To prove that static method is not dependent in the class of which it resides, try this..

class Test

    public function non_s(){
        echo 'not static method';
        self::static_m(); // this will work
        static::static_x(); //this will work


    public static function static_m(){

        echo 'this is from static method static_m <br/>';
        self::non_s(); // this will throw an error
        $this->non_s(); // this will not work also


     public static function static_x(){
         echo 'This from static_x();
         $this->static_m();// will throw a not in object context error. 


   ## this will work

   ## this one will not work for the non-static
   Test::non_s(); //will not work

   ## but this will work
   $object = new Test();
   $object::static_x();// this will work
   $object->static_x(); // this will work also

In conclusion, static method can be access regardless if there is an instance of object or not.

thx very much for your explanation
I have another question if you please:
how to call a method(a class) inside a method(another class) regardless of static methods ?

That's pretty easy really. Let say we have class A and class B both with non-static and static methods. We want class B to use some of the class A's methods and class A doing the same for the class B statics both in static and object forms.

example. Here we go .. this is a brain teaser for you and others. Make sure to have some sour gummy bear or lots of soda and coffee. I am not responsible if this will give you a headache.


     class A

        public function __construct(){}

        public function method_a(){
            echo 'this is non-static method_a from class A <br/>';


        public static function method_b(){
            echo 'this is static method_b from class A <br/>';


        public static function use_class_b(){

            echo B::method_d().'<br/>';


    class B

        public function construct(){}

        public function method_c(){

            $object_a = new A();

            return array(A::method_b(),$object_a->method_a(), $object_a->method_b(), $object_a::method_b());


         public static function method_d(){

             $object_a = new A();
             return $object_a->method_b();



  echo 'Demonstration of class B => static method_d with an object of class A :';
  echo '<br/>'; 
  echo 'Demonstration of Class B as object => method_d and method_c : ';

  $object_b = new B();
  $object_b->method_d().' and method_c<br/>'; 

  echo 'This is from method_d with an array output :<br/>';
  $object_b->method_c()[0].' ,'. $object_b->method_c()[1] .','. $object_b->method_c()[2].','.$object_b->method_c()[3].'<br/>';

  echo '<br/>';
  echo 'Demonstration of class A as object<br/>'; 
  echo 'the static mode : ';

  echo 'The static use_class_b : ';
  echo '<br/>';

  echo 'The Object of A <br/>';
  $object_a = new A();