0
I can’t think of the right structure to create application in PHP.
At first I have the Person and Student classes. The Student class instances the Person class in order to use its attributes and methods of course. Characterizing an inheritance by composition. Note: I have always done direct inheritance with the use of extends, but I want to change this by isolating classes better.
But I’m lost in the midst of some mistakes.
- In the Person class I try to make a generic select that will be used in the Student class, however I have errors in what appears to be the instantiation of the Connection class.
What I want beyond understanding the structure is to correctly use the composition model.
Using eclipse + Mysql + Apache
follows code:
CLASS PERSON
<?php
namespace site\modulo1;
include_once 'init.php';
class Pessoa
{
private $cpf;
private $rg;
private $nome;
private $data_nascimento;
private $endereco;
private $bairro;
private $cidade;
private $estado;
private $pais;
private $cep;
function setPessoa( $cpf, $rg, $nome, $data_nascimento, $endereco,
$bairro,$cidade, $estado, $pais, $cep){
$this->cpf= $cpf;
$this->rg = $rg;
$this->nome = $nome;
$this->data_nascimento = $data_nascimento;
$this->endereco = $endereco;
$this->bairro = $bairro;
$this->cidade = $cidade;
$this->estado = $estado;
$this->pais = $pais;
$this->cep = $cep;
}
function getPessoa($table){
$sql = "SELECT cpf, rg, nome, data_nascimento, endereco,
bairro,cidade, estado, pais, cep FROM $table";
$q='';
$q->exec(sql);
foreach ($q as $row) {
$data[]=$q;
}
return $data;
// $connexao->closeConnection();
}
function postPessoa($table){
$dados = array($this->cpf, $this->rg, $this->nome, $this->data_nascimento, $this->endereco, $this->bairro, $this->cidade, $this->estado, $this->pais, $this->cep);
$conn->prepare("INSERT INTO $table (cpf, rg, nome, data_nascimento, endereco, bairro,cidade, estado, pais, cep)
VALUES (:cpf, :rg, :nome, :data_nascimento, :endereco, :bairro,:cidade, :estado, :pais, :cep)")->execute($dados);
unset($conn);
}
function updatePessoa($table){
}
function deletePessoa($table){
}
}
STUDENT CLASS
<?php
namespace site;
use site\modulo1\Pessoa;
include_once 'site/modulo1/Pessoa.php';
class Aluno
{
private $matricula;
private $Pessoa;
function __construct() {
$this->Pessoa = new Pessoa();
}
public function setAluno( $cpf, $rg, $nome, $data_nascimento,
$endereco, $bairro,$cidade, $estado, $pais, $cep){
$this->Pessoa->setPessoa($cpf, $rg, $nome, $data_nascimento,
$endereco, $bairro, $cidade, $estado, $pais, $cep);
}
public function postAluno(){
$this->Pessoa->postPessoa();
}
public function listaAlunos(){
$this->Pessoa->getPessoa('alunos');
}
/**
* @return mixed
*/
public function getMatricula()
{
return $this->matricula;
}
/**
* @return \site\modulo1\Pessoa
*/
public function getPessoa()
{
return $this->Pessoa;
}
/**
* @param mixed $matricula
*/
public function setMatricula($matricula)
{
$this->matricula = $matricula;
}
/**
* @param \site\modulo1\Pessoa $Pessoa
*/
public function setPessoa($Pessoa)
{
$this->Pessoa = $Pessoa;
}
}
?>
<div class="panel panel-warning">
<div class="panel-heading">
<h3>Listando Alunos</h3>
</div>
<div class="panel-body">
<a href="index.php?pagina=site_modulo1_Aluno" class="btn btn-
primary"> <span class="glyphicon glyphicon-plus"> </span> Novo
Aluno</a>
<br><br>
<table class="listagem table table-bordered table-striped
table-responsive">
<thead>
<tr>
<th>matricula</th>
<th>cpf</th>
<th>rg</th>
<th>nome</th>
<th>data nascimento</th>
<th>endereco<th>
<th>bairro</th>
<th>cidade</th>
<th>estado</th>
<th>pais</th>
<th>cep</th>
<th width="90px">Editar</th>
<th width="90px">Excluir</th>
</tr>
</thead>
<tbody>
<?php $Aluno = new Pessoa();
$Aluno->getPessoa('alunos');
Foreach ($results as $Aluno) { ?>
<tr>
<td><b><?php echo $Aluno->getMatricula();?></b></td>
<td><?php echo $Aluno->getCpf();?></td>
<td><?php echo $Aluno->getRG() ?></td>
<td><a href="index.php?pagina=estados_editar&id=<?php echo $Aluno->getMatricula;?>">Editar</a></td>
<td><a href="index.php?pagina=estados_excluir&id=<?php echo $Aluno->getMatricula;?>">Excluir</a></td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr>
<th colspan="5" class="text-center"><?php echo date('d/m/Y h:i:s'); ?></th>
</tr>
</tfoot>
</table>
</div>
CONNECTION CLASS
<?php
namespace site;
use site\modulo1\Pessoa;
include_once 'site/modulo1/Pessoa.php';
/**
* @author developer Fabiano
*
* relação de composição com classe Pessoa;
*/
class Aluno
{
private $matricula;
private $Pessoa;
function __construct() {
$this->Pessoa = new Pessoa();
}
public function setAluno( $cpf, $rg, $nome, $data_nascimento, $endereco, $bairro,$cidade, $estado, $pais, $cep){
$this->Pessoa->setPessoa($cpf, $rg, $nome, $data_nascimento, $endereco, $bairro, $cidade, $estado, $pais, $cep);
}
public function postAluno(){
$this->Pessoa->postPessoa();
}
public function listaAlunos(){
$this->Pessoa->getPessoa('alunos');
}
GETTERS AND SETERS
}
?>
Honestly, I see no reason to do this. Inheritance and composition are different things, right? And I don’t see why to use the composition here. A student is composed of a person? It makes no sense. A student is a person, so the correct would be inheritance, not composition. Forcing the composition in this case makes the code not make sense semantically. If you want to use it correctly, use inheritance, not composition.
– Woss
Correct. I believe I should use the composition method with another senario. I actually have the inheritance program already done. I wanted to learn to create with composition because I have difficulty using this method. And composition is different even from inheritance, I expressed myself badly.
– user65509
Can I mix the methods? The part of Pessoa’s inheritance all right is inheritance. But the other classes of the program where "fit" a composition, I have the freedom to encode as composition or even aggregation if applicable. I know that in the database modeler I can do it. It seems correct to me, no?
– user65509
Yes, you can. For example, the Car class can extend the Vehicle class with inheritance and have the Wheel class as its composition. If every Vehicle has wheels, even this Class may use such a composition.
– Woss
Great I will proceed like this. Thank you.
– user65509