Executando verificação de segurança...
1

Wrapper class para arrays numéricos em php

class MyArray
{
    private $elements;
    
    function __construct($array)
    {
        $this->elements = $array;
    }

    function __toString()
    {
        return "" . print_r($this->elements);
    }

    function sum()
    {
        return array_sum($this->elements);
    }

    function subtract()
    {
        $subtractResult = $this->elements[0];

        for ($i = 1; $i < $this->lenght(); $i++) {
            $subtractResult -= $this->elements[$i];
        }

        return $subtractResult;
    }

    function multiply()
    {

        $arrayCopy = array_merge($this->elements);
        foreach ($arrayCopy as $i => $element) {
            if ($element === 0) {
                $arrayCopy[$i] = 1;
            }
        }

        return array_product($arrayCopy);
    }

    function divide()
    {
        $divideResult = $this->elements[0];

        for ($i = 1; $i < $this->lenght(); $i++) {
            $element = $this->elements[$i];
            if ($element !== 0) {
                $divideResult /= $element;
            }
        }

        return $divideResult;
    }

    function lenght()
    {
        return count($this->elements);
    }

    function getLastItem()
    {
        return $this->elements[$this->lenght() - 1];
    }

    function getItem($index)
    {
        return $this->elements[$index];
    }

    function push($element)
    {
        $this->elements[] = $element;
    }

    function map(callable $callback)
    {
        $newArray = [];

        foreach ($this->elements as $element) {
            $newArray[] = $callback($element);
        }

        return $newArray;
    }

    function filter(callable $callback)
    {
        $newArray = [];

        foreach ($this->elements as $element) {
            if ($callback($element)) {
                $newArray[] = $element;
            }
        }

        return $newArray;
    }

    function contains($elementForSearch)
    {
        $isFound = false;

        foreach ($this->elements as $element) {
            $isFound = $element === $elementForSearch;
            if ($isFound) return $isFound;
        }

        return $isFound;
    }

    function getMax()
    {
        $max = 0;
        foreach ($this->elements as $element) {
            if ($element > $max) $max = $element;
        }
        return $max;
    }

    function getMin()
    {
        $min = 0;
        foreach ($this->elements as $element) {
            if ($element < $min) $min = $element;
        }
        return $min;
    }
}

Exemplo de utilização:

function operateNumbers($operation, ...$numbers)
{
    $myArray = new MyArray($numbers);

    $operations = [
        "+" => fn () => $myArray->sum(),
        "*" => fn () => $myArray->multiply(),
        "-" => fn () => $myArray->subtract(),
        "/" => fn () => $myArray->divide()
    ];

    return $operations[$operation]();
}

echo operateNumbers("*", 0, 2, 3, 4) . PHP_EOL;
Carregando publicação patrocinada...
3

sugestão de melhoria ?

operateNumbers(0)
->multiplyBy(2)
->substractBy(3)
->add(4)

porque o que tu fez dá pra fazer com quase 1 linha em php

$resultado = array_reduce(
  [2, 3, 4, 5],
  function ($carry, $item) {
    return $carry * $item;
  },
  1
);

echo "O resultado da multiplicação é: $resultado";