kkreft
7/31/2017 - 7:36 AM

some play with iterable

some play with iterable

<?php

class Price
{
    private $price = 0;
    
    public function __construct(iterable $prices) 
    {
        foreach ($prices as $price) {
            $this->price += $price;
        }
    }
    
    public function getPrice()
    {
        return $this->price;
    }
    
    
}

$prices = [1, 2, 3];
$price = new Price($prices);
echo $price->getPrice();

$prices = new ArrayObject($prices);
$price = new Price($prices);
echo $price->getPrice();