PHP-dä OOP-de klas içindäki statiki metodlar

Eger siz klas içinde statiki metodlary ulanmak isleseňiz, ola $this-> arkaly däl, self:: arkaly ýüzlenmeli.

Mysal üçin, bizim Math klasymyza getDoubleSum metodyny goşalyň, bu metod sanlaryň goşandysynyň iki esse tapar. Täze metod içinde öň bar bolan getSum metodyny ulanarys:

Math.php<?php
  class Math
  {
    // Iki esse goşandy tapalyň:
    public static function getDoubleSum($a, $b)
    {
      return 2 * self::getSum($a, $b); // beýle bir metodu ulanyň
    }
    
    public static function getSum($a, $b)
    {
      return $a + $b;
    }
    
    public static function getProduct($a, $b)
    {
      return $a * $b;
    }
  }
?>

Täze metody ulanyň:

index.php<?php
  echo Math::getDoubleSum(1, 2);
?>
Задание 1

Aşakdaky klasyň metodlaryny statiki metodlara öwüriň:

ArraySumHelper.php<?php
  class ArraySumHelper
  {
    public function getSum1($arr)
    {
      return $this->getSum($arr, 1);
    }
    
    public function getSum2($arr)
    {
      return $this->getSum($arr, 2);
    }
    
    public function getSum3($arr)
    {
      return $this->getSum($arr, 3);
    }
    
    public function getSum4($arr)
    {
      return $this->getSum($arr, 4);
    }
    
    private function getSum($arr, $power) {
      $sum = 0;
      
      foreach ($arr as $elem) {
        $sum += pow($elem, $power);
      }
      
      return $sum;
    }
  }
?>