Static methods in PHP OOP

Traits in PHP
Static Properties in PHP OOP

In this tutorial we will learn more about the static methods in PHP OOP. We will discuss what static methods are meant for, when to use them, the syntax and the difference between static and non-static methods.

What are static methods in PHP?

  • Static methods are created using static keyword.
  • Static methods of a class are called directly. It means, we don’t need to create the instance of the class to call its static method.
  • A class can contains both static and non-static methods.
  • To call the static method of a class, we use scope resolution operator ::

Syntax of Static Method in PHP

<?php
class ClassName {
  public static function method() {
    echo "Hello from PHP!";
  }
}
?>

Syntax to call the static method

ClassName::staticMethod();

Example

<?php
class Example {
  public static function greetings() {
    echo "Hello from PHP!";
  }
}

// Calling static method
Example::greetings();
?>
  • In the above example, we create a class that contains static method in it.
  • We call the static method of this class outside using scope resolution method.

Calling static method inside the class – PHP

  • In PHP, self keyword allows to call its own static method inside the class. Look at the following example in which we create a class with static method and call it inside the constructor of the class.
  • self::methodName() is the general syntax to call static method inside the class.
<?php
class Example {
  public static function greetings() {
    echo "Hello from PHP!";
  }

  public function __construct() {
    self::greetings();
  }
}

new Example();
?>

Calling Static Method inside another class – PHP

It’s very interesting to use case of static methods, that we can call a public static method inside another class. Remember, it should be a public static method.

<?php
class Example{
  public static function greetings() {
    echo "Hello from PHP!";
  }
}

class Example2{
  public function msg() {
    Example::greetings();
  }
}
?>

Calling a static method in child class – PHP

In the case of inheritance, we can call the static method of the parent class inside the child class using the parent keyword. Look at the example below.

<?php
class Example {
  protected static function getMessage() {
    return "Message from static method";
  }
}

class Child1 extends domain {
  public $name;
  public function __construct() {
    $this->name= parent::getMessage();
  }
}

$child1= new child1();
echo $child1-> name;
?>
  • In the above example, we create a parent class with some static method.
  • Also, we create another class that extends the parent class.
  • Call the static method of the parent class using parent keyword followed by the name of the static method.

Note: As we mentioned in the previous section that calling a static method inside another class required it to be public. However, in the case of calling the static method of the parent class (inheritance), the static method access modifier can be public or protected.

Difference between Static and Non Static Methods

Static MethodNon Static Method
We can access only static properties of the class or of another class inside the static method.We can access both the static and non-static properties of the class or of another class inside the non-static method.
Static Method uses early binding or compile-time binding.The non-Static method uses dynamic or runtime binding.
Static methods maintain the state during the execution of the program, so due to early binding, they cannot be overridden by another class.Due to the runtime binding or dynamic behavior of the non-static method, we can override them in another class.
Memory allocation in the static method is more efficient because it specifies a memory block inside the ram once the program is executed. It never does it again until the program terminates.Non-static methods are less efficient in memory allocation because of dynamic binding. Every time the non-static method is called, it allocates memory and deallocates after the execution of the method completes.
Static vs Non-Static Method

Reference to the official PHP documentation of static method.

Traits in PHP
Static Properties in PHP OOP

Stay up-to-date about PHP!

We don’t spam!

en English
X
Scroll to Top