Exercício __set
Dentro da Classe Address
<?php
namespace Alura\Bank\Model;
/**
* Class Addres
* @package Alura\Bank\Model;
* @property string $city;
* @property string $neighborhood;
* @property string $street;
* @property string $houseNumber;
*/
class Address
{
private string $city;
private string $neighborhood;
private string $street;
private string $houseNumber;
public function __construct(string $citty, string $neighborhood, string $street, string $houseNumber)
{
$this->city = $citty;
$this->neighborhood = $neighborhood;
$this->street = $street;
$this->houseNumber = $houseNumber;
}
public function setCity(string $city): void
{
$this->city = $city;
}
public function setNeighborhood(string $neighborhood): void
{
$this->neighborhood = $neighborhood;
}
public function setStreet(string $street): void
{
$this->street = $street;
}
public function setHouseNumber(string $houseNumber): void
{
$this->houseNumber = $houseNumber;
}
public function getCity(): string
{
return $this->city;
}
public function getNeighborhood(): string
{
return $this->neighborhood;
}
public function getStreet(): string
{
return $this->street;
}
public function getHouseNumber(): string
{
return $this->houseNumber;
}
public function __toString(): string
{
return "$this->street, $this->houseNumber, $this->neighborhood, $this->city." . PHP_EOL;
}
public function __get($nameAttribute)
{
$method = 'get' . ucfirst($nameAttribute);
return $this->$method() . PHP_EOL;
}
public function __set($nameAttribute, $value)
{
$method = 'set' . ucfirst($nameAttribute);
$this->$method($value);
}
}
Dentro do arquivo addresses
<?php
use Alura\Bank\Model\Address;
require_once 'autoload.php';
$setEndereco = new Address('São Paulo', 'Sei lá', 'Jardim Sei lá', '100');
echo 'Antes de mudar' . PHP_EOL;
echo '-------------------------------------------------' . PHP_EOL;
echo '- '.$setEndereco;
echo '-------------------------------------------------' . PHP_EOL;
$setEndereco->city = 'SeiLaLandia';
$setEndereco->neighborhood = 'Vila Zero';
$setEndereco->street = 'Rua de joão ninguém';
$setEndereco->houseNumber = '0A';
echo 'Depois de mudar'. PHP_EOL;
echo '-------------------------------------------------' . PHP_EOL;
echo '- '.$setEndereco;
echo '-------------------------------------------------' . PHP_EOL;
Resultado