<?php
namespace Alura\Banco\Modelo;
class Endereco{
private $cidade;
private $bairro;
private $rua;
private $numero;
function __construct(string $cidade,string $bairro,string $rua,string $numero){
$this->cidade = $cidade;
$this->bairro = $bairro;
$this->rua = $rua;
$this->numero = $numero;
}
public function recuperaCidade(){
return $this->cidade;
}
public function recuperaBairro(){
return $this->bairro;
}
public function recuperaRua(){
return $this->rua;
}
public function recuperaNumero(){
return $this->numero;
}
public function __toString() :string{
return "{$this->rua}, {$this->numero}, {$this->bairro}, {$this->cidade}";
}
//Metodo apenas para recuperar e nao para atribuir
public function __get(string $nomeAtributo){
$metodo = 'recupera' . ucfirst($nomeAtributo);
return $this->$metodo();
}
public function __set($name, string $value):void
{
$this->$name = $value;
}
}
<?php
use Alura\Banco\Modelo\Endereco;
require_once 'autoload.php';
$endereco1 = new Endereco('Sao paulo','Qualquer','minha rua','22');
$endereco2= new Endereco('Rio','Centro', 'Rua BalaTensa','12');
// echo $endereco1 . PHP_EOL;
// echo $endereco2 . PHP_EOL;
echo $endereco2->rua = 'Gabriel';
echo $endereco2 . PHP_EOL;