PHP Static Properties Explained

Last Updated: 27 Dec, 2024

What are static properties in PHP?

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

Points to note:

  • Static properties in PHP are declared with the static keyword.
  • Static properties are public by default unless you explicitly declare it as private or protected.
  • Static properties are accessed with the help of the Scope Resolution Operator (::).
  • Static properties can not be accessed using the Object Operator (->).

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

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

Example 2: access static properties using self keyword and member function

<?php
class Car
{
    public static $brand = 'Tata';
    public static $model = 'Tata Safari';

    public function getBrand()
    {
        return self::$brand;
    }

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

Example 3: access static properties using self keyword and static function

<?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
$properties = array(
    'brand' => Car::getBrand(),
    'model' => Car::getModel()
);
print_r($properties);
/* OUTPUT
Array ( [brand] => Tata [model] => Tata Safari )
*/
?>

Example 4: access static properties from parent class within child class using self keyword, parent keyword and static function

<?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 extends Car
{
    public static $transmission = 'Automatic';

    public function __construct()
    {
        parent::__construct();
    }

    public static function getTrasmission()
    {
        return self::$transmission;
    }
}

// get static properties
$properties = array(
    'brand' => MyCar::getBrand(),
    'model' => MyCar::getModel(),
    'transmission' => MyCar::getTrasmission()
);
print_r($properties);
/* OUTPUT
Array ( [brand] => Tata [model] => Tata Safari [transmission] => Automatic )
*/
?>

 

Thank You, Please Share.

Recommended Posts

IMAGE

PHP OOP Access Modifiers Explained

PHP access modifiers allow you to control where your class members can be accessed from, for instance to prevent a certain variable from being modified from outside the class.

IMAGE

PHP OOP Traits Explained

Traits in PHP, are a medium of implementing code reuse and allow us to reuse sets of functions freely in several independent classes.

IMAGE

PHP Static Methods Explained

The static methods in PHP are those methods which are declared with the static keyword and can be accessed directly using Scope Resolution Operator (::).