PHP OOPS difference between calling the class methods statically and by class instantiation with example and walk through


Before going further letme tell you class instantiation is just used to access all the content inside that class, say class methods,attributes, variables. constants can be accessed statically

To use the instantiation, Key thing is  just remember for instantiation we use this symbol -> (pointer) and for anything you call statically we use :: scope resolution operator.

Basically we have two methods to call the class methods

One is Instantiation method 

$callingNonStaticPublicMethods = new A();

$callingNonStaticPublicMethods->functiona();

Another one is statically calling method.

A::functiona();


Follow me I'm sure at the end you will know the difference between static and instantiation

Lets discuss this with an example, do it in your local machine with me.

1. Add these two line at the top of your file these lines are going to show you all.

<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);

Define Class A

class A {
	public function functiona() {
      echo "This is non static public method a \n";
     
	}

	 public static function staticFunction() {
	  echo "This is static public method \n";
	}

	public function functionb() {
     echo "This is non static public method b \n";
	}	
	
} 

We does not have any thing in the methods, we just printed something,

Calling class method statically using scope resolution opereter

A::staticFunction(); // prints as it is 
A::functiona(); //Strict Standards:Non-static method A::functiona() should not be called statically

 Now I'm going to comment the Above two lines and going to call by Instantiation method.

$callingNonStaticPublicMethods = new A();

$callingNonStaticPublicMethods->functiona(); // this will print 

$callingNonStaticPublicMethods->staticFunction(); // this will print 

 

2. Now call the functionb inside the functiona 

class A {
	public function functiona() {
     echo "This is non static public method a \n";
     $this->functionb();
	}

	 public static function staticFunction() {
	 echo "This is static public method \n";

	}

	public function functionb() {
     echo "This is non static public method b \n";
	}	
	
} 

Calling using Instantiation Method

$callingNonStaticPublicMethods = new A();

$callingNonStaticPublicMethods->functiona(); // this will print 
$callingNonStaticPublicMethods->functionb(); // this will print
$callingNonStaticPublicMethods->staticFunction(); // this will print 

Now Im going to comment the Above 4 lines and going to call by  Static method

Calling class method statically using scope resolution operator

A::staticFunction(); // prints as it is 
A::functiona(); //Fatal error: Using $this when not in object context in  

There is no instantiation of the class objects so its throws error when calling the "functionb"

3. Now remove the line calling functionb ($this->functionb();) from fucntiona and add it to staticFunction Method   

class A {
	public function functiona() {
     echo "This is non static public method a \n";
	}

	 public static function staticFunction() {
	 echo "This is static public method \n";
	 $this->functionb();
	}

	public function functionb() {
     echo "This is non static public method b \n";
	}	
	
} 

Now call the class methods by  Instantiation

$callingNonStaticPublicMethods = new A();

$callingNonStaticPublicMethods->functiona(); // this will print 
$callingNonStaticPublicMethods->functionb();
$callingNonStaticPublicMethods->staticFunction(); // //Fatal error: Using $this when not in object context in 

Now Im going to comment the Above 4 lines and going to call by  Static method

// Calling class method statically using scope resolution operater
A::functiona();  //Strict Standards:Non-static method A::functiona() should not be called statically
A::staticFunction(); // //Fatal error: Using $this when not in object context in

5. Now  call the staticFunction in public functiona 

class A {
	public function functiona() {
     echo "This is non static public method a \n";
     $this->functionb();
     $this->staticFunction(); //Fatal error: Using $this when not in object context in
	}

	 public static function staticFunction() {
	 echo "This is static public method \n";
	}

	public function functionb() {
     echo "This is non static public method b \n";
	}	
	
} 

Now the call the class methods by  Instantiation

$callingNonStaticPublicMethods = new A();

$callingNonStaticPublicMethods->functiona(); // this will print 
$callingNonStaticPublicMethods->staticFunction(); // this will print

Now Im going to comment the Above 3 lines and going to call by  Static method

// Calling class method statically using scope resolution operater
A::staticFunction(); // this will print
A::functiona(); // Strict Standards: Non-static method A::functiona() should not be called statically in 

6. Now  call the staticFunction in public functiona statically

class A {
	public function functiona() {
     echo "This is non static public method a \n";
     self::staticFunction(); 
     $this->functionb(); // Fatal error: Using $this when not in object context
	}

	 public static function staticFunction() {
	 echo "This is static public method \n";
	}

	public function functionb() {
     echo "This is non static public method b \n";
	}	
	
} 

Now the call the class methods by  Instantiation

$callingNonStaticPublicMethods = new A();

$callingNonStaticPublicMethods->functiona(); // this will print 
$callingNonStaticPublicMethods->staticFunction(); // this will print

Now Im going to comment the Above 3 lines and going to call by  Static method

// Calling class method statically using scope resolution operater
A::staticFunction(); // this will print
A::functiona(); // Strict Standards: Non-static method A::functiona() and Fatal error: Using $this when not in object context 


 

Note :

1. self keyword is used to access the members and attributes statically, where in $this refers to the current object instance and self refers to current class.  

2. $this cannot be used in static member functions because static methods cannot know which instance you using to access it as you just call it by classname::functionname()

3. When you instantiate the class you could use $this to access it members functions and attributes, also other static functions can be called from your public non-static method by self::functioname()

Hopes now you know the difference between Static and instantiation :)