DEV Community

vimuth
vimuth

Posted on • Edited on

PHP static methods.

In this tutorial we are doing comparison with static and non static methods. Static functions are methods that are defined to be accessible on the class itself rather than on instances of the class. They can be called without creating an object instance of a class.

Comparison with non static methods

Example of Non-Static Method

class Calculator {
    public function add($a, $b) {
        return $a + $b;
    }
}

// Creating an instance of Calculator
$calc = new Calculator();
echo $calc->add(5, 3);  // Outputs: 8

Enter fullscreen mode Exit fullscreen mode

Example of Static Method

class Calculator {
    public static function add($a, $b) {
        return $a + $b;
    }
}

// Calling the static method
echo Calculator::add(5, 3);  // Outputs: 8

Enter fullscreen mode Exit fullscreen mode

Static Methods and Constructors

Static methods can't use constructors directly since we are not creating child objects. (Eg: new Object())

While you can’t call constructors directly in static methods, you can create new instances of objects (which will invoke constructors) within static methods. Here’s how you might do it in PHP:

class Person {
    public $name;
    public $age;

    public function __construct($name, $age) {
        $this->name = $name;
        $this->age = $age;
    }

    public static function createTeenager($name) {
        return new Person($name, 13);  // Constructor is called here
    }
}

$teen = Person::createTeenager("Alice");
echo $teen->name; // Outputs: Alice

Enter fullscreen mode Exit fullscreen mode

Static Properties.

Non-Static Property

In non static methods we can do like this,

<?php

class Person {
    // Declare a public property
    public $name;

    // Method to set the name property
    public function setName($name) {
        $this->name = $name;
    }

    // Method to get the name property
    public function getName() {
        return $this->name;
    }
}

$person = new Person();

$person->setName("Alice");
echo "The person's name is " . $person->getName();  
?>
Enter fullscreen mode Exit fullscreen mode

public $name; here is called non static property or instance property. You must instantiate the class to use them.
Each object or instance of the class has its own separate copy of this property (public $name; here). Changes made to the property affect only that specific instance.

Static Property

Also known as a "static variable" or "class variable," this property is shared among all instances of the class. It belongs to the class itself rather than to any one instance.

class Person {
    public static $name;  // Static property

    public static function setName($name) {
        self::$name = $name;  // Set the shared name property
    }

    public static function getName() {
        return self::$name;  // Return the shared name
    }
}

Person::setName("Charlie");
echo Person::getName(); // Outputs: Charlie

$person1 = new Person();
$person2 = new Person();

echo $person1::getName(); // Outputs: Charlie
echo $person2::getName(); // Outputs: Charlie
Enter fullscreen mode Exit fullscreen mode

Using Static Properties with static methods.

class Person {
    public static $name;

    public static function setName($name) {
        self::$name = $name;  // Setting a static property
    }

    public static function getName() {
        return self::$name;  // Accessing a static property
    }
}

Person::setName('Tester');
echo Person::getName(); // Outputs: Tester
Enter fullscreen mode Exit fullscreen mode

We can use these static properties with static methods since they are not specific to an instance.

Why Static Methods are faster?

1.No Object Instantiation:

You don't need to create objects of classes like this,

$person = new Person(); 
Enter fullscreen mode Exit fullscreen mode

Less lines of codes means it runs faster.

2.Less Memory Usage
Since static methods do not require object instantiation, they save memory that would otherwise be used to store the state of objects.

Top comments (0)