PHP Static Methods Explained

Last Updated: 24 Jul, 2024

What are static methods in PHP?

The static methods or functions are those methods which are declared with the static keyword. All static methods are public by default, but you can make them private or protected if required. You can access static methods directly using Scope Resolution Operator (::) along with the class name. These methods are not accessible by its object with the help of Object Operator (->)

Points to note:

  • Static methods are declared with the static keyword.
  • Static methods are public by default unless you explicitly declare it as private or protected.
  • Static methods are accessed with the help of the Scope Resolution Operator (::).
  • Static methods can not be accessed using the Object Operator (->).
  • Static methods can not access non-static properties or methods because they require an instance of the class to be created

Example 1: access static properties using static method and scope resolution operator (::)

<?php
class Car
{
	public static $brand = 'Tata';
	public static $model = 'Tata Safari';
	public static function getBrand()
	{
		return self::$brand;
	}
	public static function getModel()
	{
		return self::$model;
	}
}
// get static properties using static method and scope resolution operator (::)
$brand = Car::getBrand();
$model = Car::getModel();
$properties = array(
	'brand' => $brand,
	'model' => $model
);
print_r($properties);
/* OUTPUT
Array ( [brand] => Tata [model] => Tata Safari )
*/
?>

In above example, we have declared two static properties $brand and $model. We also defined two static methods called getBrand() and getModel(). Then we are calling both the static methods using class name and  Scope Resolution Operator (::) without creating any instance of Car class.

As we know, a class can have both static and non-static methods inside it. So, we can even access the static methods inside a non-static method with the help of self keyword and Scope Resolution Operator (::).

Example 2: access static methods using non-static method and scope resolution operator (::)

<?php
class Car
{
    public static $brand = 'Tata';
    public static $model = 'Tata Safari';
    public static function getBrand()
    {
        return self::$brand;
    }
    public static function getModel()
    {
        return self::$model;
    }
    public function showProperties()
    {
        $properties = array(
            'brand' => self::getBrand(),
            'model' => self::getModel()
        );
        return $properties;
    }
}
// get static properties using non-static method and scope resolution operator (::)
$car = new Car();
$properties = $car->showProperties();
print_r($properties);
/* OUTPUT
Array ( [brand] => Tata [model] => Tata Safari )
*/
?>

Even we can call static methods of one class from methods in another class. The only restriction is that the static methods must be declared as public.

Example 3: access static methods from methods in another class

<?php
class Car
{
    public static $brand = 'Tata';
    public static $model = 'Tata Safari';
    public static function getBrand()
    {
        return self::$brand;
    }
    public static function getModel()
    {
        return self::$model;
    }
}
class MyCar 
{
    public function showProperties()
    {
        $properties = array(
            'brand' => Car::getBrand(),
            'model' => Car::getModel()
        );
        return $properties;
    }
}
$mycar = new MyCar();
$properties = $mycar->showProperties();
print_r($properties);
/* OUTPUT
Array ( [brand] => Tata [model] => Tata Safari )
*/
?>

We can also call a static method from a child class with the help of parent keyword inside the child class. In this case, static method can be either public or protected.

Example 4: access static methods from child calss using parent keyword

<?php
class Car
{
    public static $brand = 'Tata';
    public static $model = 'Tata Safari';
    public static function getBrand()
    {
        return self::$brand;
    }
    protected static function getModel()
    {
        return self::$model;
    }
    protected static function getTransmission()
    {
        return 'Automatic';
    }
}
class MyCar extends Car
{
    public $transmission;
    public function __construct()
    {
        $this->transmission = parent::getTransmission();
    }
    public function showProperties()
    {
        $properties = array(
            'brand' => Car::getBrand(),
            'model' => Car::getModel(),
            'transmission' => $this->transmission
        );
        return $properties;
    }
}
$mycar = new MyCar();
$properties = $mycar->showProperties();
print_r($properties);
/* OUTPUT
Array ( [brand] => Tata [model] => Tata Safari [transmission] => Automatic )
*/
?>

 

Thank You, Please Share.

Recommended Posts

IMAGE

PHP OOP Interfaces Explained

An Interface allows you to create programs that specifies which methods a class must implement, without defining how those methods are implemented.

IMAGE

PHP OOP Abstract Classes Explained

A PHP class that has minimum one abstract method is called as abstract class and cannot be instantiated by itself.

IMAGE

PHP Static Properties Explained

The static properties or variables are those type of variables which are defined with the static keyword.