Static Methods in OOP in PHP
When working with classes, you can create methods
that do not require creating an object for their
call. Such methods are called static.
To declare a method static, you need to write
the keyword static
after the access modifier:
<?php
class Test
{
public static function method()
{
return '!!!';
}
}
?>
To access a static method, you need to write the class name, then two colons, and the method name. You do not need to create an object of the class, like this:
<?php
echo Test::method(); // will output '!!!'
?>
Convert the methods of the following class into static ones:
<?php
class Math
{
public function getSum($a, $b)
{
return $a + $b;
}
public function getProduct($a, $b)
{
return $a * $b;
}
}
?>