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.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
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 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 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:
181.22 ms | 402 KiB | 182 Q