PHP Static Methods
Static methods in PHP are methods that belong to the class rather than any particular object. They can be called without creating an instance of the class.
Defining a Static Method
<?php
class Math {
public static function add($a, $b) {
return $a + $b;
}
}
?>
Explanation: This example defines a class Math
with a static method add
that takes two parameters and returns their sum.
Calling a Static Method
<?php
$result = Math::add(5, 10);
echo $result; // Outputs: 15
?>
Explanation: This code demonstrates how to call the static method add
from the Math
class without creating an object.