Last active
October 6, 2023 15:12
-
-
Save wesllycode/fc84ca2a877b20b4e005e5125f5ad976 to your computer and use it in GitHub Desktop.
Criando classes com PHP usando OOP com e sem interface, agregação de relacionamento de classes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Pessoa { | |
public function __construct( | |
private String $nome, | |
private String $sobrenome, | |
private String $endereco) | |
{ | |
} | |
public function getDadosPessoa() | |
{ | |
$nome = $this->nome; | |
$sobrenome = $this->sobrenome; | |
$endereco = $this->endereco; | |
return 'Meu nome é '.$nome.' e meu sobrenome é '.$sobrenome.', e moro no endereco'.$endereco; | |
} | |
} | |
$pessoa = new Pessoa('weslly','code','Rua caeté, 1200, bairro Centro, Sobral, Ceará'); | |
var_dump($pessoa->getDadosPessoa()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Endereco { | |
public function __construct( | |
private String $rua, | |
private Int $numero, | |
private String $bairro, | |
private String $cidade, | |
private String $estado) | |
{ | |
} | |
public function getNome() | |
{ | |
return 'Moro na,'.$this->rua.', '.$this->numero.', '.$this->bairro.', '.$this->cidade.', '.$this->estado; | |
} | |
} | |
class Pessoa { | |
private $endereco; | |
public function __construct( | |
private $nome, | |
private $sobrenome, | |
) | |
{ | |
} | |
public function setEndereco(Endereco $end) | |
{ | |
$this->endereco = $end; | |
} | |
public function getEndereco() | |
{ | |
return $this->endereco; | |
} | |
public function getDadosPessoa() | |
{ | |
$nome = $this->nome; | |
$sobrenome = $this->sobrenome; | |
return 'Meu nome é '.$nome.' e meu sobrenome é '.$sobrenome; | |
} | |
} | |
$pessoa = new Pessoa('weslly','code'); | |
$endereco = new Endereco ('Rua caeté', '1200', 'Centro', 'Sobral', 'Ceará'); | |
$pessoa->setEndereco($endereco); | |
print $pessoa->getDadosPessoa(); | |
print ' '; | |
/* | |
No livro de PHP do Pablo, orientado objetos, esse é um relacionamento entre objetos do tipo associação. Ele descreve, que | |
na associação um objeto contém referência de outro objeto. Um objeto terá atributo que apontará para a posição da memória onde | |
o outro objeto se encontra, podendo executar seus métodos. È exatamente isso que eu faço abaixo. | |
*/ | |
print $pessoa->getEndereco()->getNome(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Exemplo de relacionamento entre classes do tipo composição */ | |
/* Ainda não está completo */ | |
Class Caracteristica | |
{ | |
public function __construct( | |
private $nome, | |
private $descricao) | |
{ | |
} | |
public function getNome() | |
{ | |
return $this->nome; | |
} | |
public function getDescricao() | |
{ | |
return $this->descricao; | |
} | |
} | |
Class Casa{ | |
private $carateristicas; | |
public function __construct( | |
private Pessoa $dono, | |
private Endereco $end | |
) | |
{ | |
} | |
public function getDono() | |
{ | |
return $this->dono; | |
} | |
public function getEndereco() | |
{ | |
return $this->end; | |
} | |
public function addCat($nome, $descricao) | |
{ | |
$this->carateristicas[] = new Caracteristica($nome, $descricao); | |
} | |
public function getCat() | |
{ | |
return $this->carateristicas; | |
} | |
} | |
class Endereco { | |
public function __construct( | |
private String $rua, | |
private Int $numero, | |
private String $bairro, | |
private String $cidade, | |
private String $estado) | |
{ | |
} | |
public function handle() | |
{ | |
return ' Moro na,'.$this->rua.', '.$this->numero.', '.$this->bairro.', '.$this->cidade.', '.$this->estado; | |
} | |
} | |
class Pessoa { | |
public function __construct( | |
private String $nome | |
) | |
{ | |
} | |
} | |
$pessoa = new Pessoa('weslly'); | |
$endereco = new Endereco ('Rua caeté', '1200', 'Centro', 'Sobral', 'Ceará'); | |
$casa = new Casa($pessoa, $endereco); | |
//Resultado | |
print_r($casa->getDono()); | |
print_r($casa->getEndereco()); | |
$casa->addCat('cor','vermelha'); | |
$casa->addCat('area quadrada', '2 metros ao quadrado'); | |
$casa->addCat('valor', 'R$ 10000,00'); | |
foreach($casa->getCat() as $cat) | |
{ | |
print 'Caracteristica:'. $cat->getNome().' - '. $cat->getDescricao().PHP_EOL; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
public function setEndereco(Endereco $end) { $this->endereco = $end; }