3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Carro implements IteratorAggregate { private Peca $motor; private Peca $pneus; private Peca $amortecedor; public function __construct( Peca $motor, Peca $pneus, Peca $amortecedor ) { $this->motor = $motor; $this->pneus = $pneus; $this->amortecedor = $amortecedor; } public function __get($atributo) { return $this->$atributo; } public function __set($atributo, $valor) { $this->$atributo = $valor; } public function getIterator() { return new \ArrayIterator([ $this->motor, $this->pneus, $this->amortecedor, ]); } } class Peca { private string $marca; private string $modelo; private float $preco; public function __construct( ?string $marca, string $modelo, float $preco ) { $this->marca = $marca; $this->modelo = $modelo; $this->preco = $preco; } public function __get($atributo) { return $this->$atributo ; } public function __set($atributo, $valor) { $this->$atributo = $valor; } } $carro = new Carro( new Peca('Teste', "1.8", 15000.00), new Peca("Pirelli", "P7", 500.00), new Peca("Cofap", "ar", 1000.00) ); foreach($carro as $peca){ $peca->marca = "VW"; } var_dump($carro);
Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
Deprecated: Return type of Carro::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/4HmUZ on line 29 object(Carro)#1 (3) { ["motor":"Carro":private]=> object(Peca)#2 (3) { ["marca":"Peca":private]=> string(2) "VW" ["modelo":"Peca":private]=> string(3) "1.8" ["preco":"Peca":private]=> float(15000) } ["pneus":"Carro":private]=> object(Peca)#3 (3) { ["marca":"Peca":private]=> string(2) "VW" ["modelo":"Peca":private]=> string(2) "P7" ["preco":"Peca":private]=> float(500) } ["amortecedor":"Carro":private]=> object(Peca)#4 (3) { ["marca":"Peca":private]=> string(2) "VW" ["modelo":"Peca":private]=> string(2) "ar" ["preco":"Peca":private]=> float(1000) } }
Output for 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
object(Carro)#1 (3) { ["motor":"Carro":private]=> object(Peca)#2 (3) { ["marca":"Peca":private]=> string(2) "VW" ["modelo":"Peca":private]=> string(3) "1.8" ["preco":"Peca":private]=> float(15000) } ["pneus":"Carro":private]=> object(Peca)#3 (3) { ["marca":"Peca":private]=> string(2) "VW" ["modelo":"Peca":private]=> string(2) "P7" ["preco":"Peca":private]=> float(500) } ["amortecedor":"Carro":private]=> object(Peca)#4 (3) { ["marca":"Peca":private]=> string(2) "VW" ["modelo":"Peca":private]=> string(2) "ar" ["preco":"Peca":private]=> float(1000) } }
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33
Parse error: syntax error, unexpected 'Peca' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in /in/4HmUZ on line 5
Process exited with code 255.

preferences:
160.71 ms | 410 KiB | 5 Q