1
I have a class called cliente
class cliente {
private $id;
private $nome;
function getId() {
return $this->id;
}
function getNome() {
return $this->nome;
}
function setId($id) {
$this->id = $id;
}
function setNome($nome) {
$this->nome = $nome;
}
}
And two other classes called selecionaCliente
and insereCliente
these two extend the class cliente
:
<?php
require_once 'cliente.php';
class insereCliente extends cliente{
private $pdo;
function __construct() {
require_once 'conbdd.php';
$db = new conbdd;
$this->pdo = $db->conectar();
}
function selId(){
try{
$sel=$this->pdo->prepare("SELECT name FROM clientes WHERE
id=:id");
$sel->bindValue(":id", $this->getId());
$sel->execute();
$temp=$sel->fetch();
$this->setNome($temp['nome']);
return TRUE;
} catch (PDOException $ex){
echo $ex->getMessage();
return FALSE;
}
}
<?php
require_once 'cliente.php';
class selecionaCliente extends cliente{
private $pdo;
function __construct() {
require_once 'conbdd.php';
$db = new conbdd;
$this->pdo = $db->conectar();
}
function insNome(){
try {
$ins= $this->pdo->prepare("UPDATE clientes SET nome=:nome WHERE
id=:id");
$ins->bindValue(":id", $this->getId());
$ins->bindValue(":nome", $this->getNome());
$ins->execute();
return TRUE;
} catch (PDOException $ex) {
echo $ex->getMessage();
return FALSE;
}
}
}
I want to validate and store customer data within the class cliente
through the class that will use the information
If I instantiate both classes selecionaCliente
and insereCliente
and set an information in the selecionaCliente
I will be able to retrieve this information through the instance of the class insereCliente
, or each instance creates a different client? If so, how could I make all classes use the same instance of cliente
?
selecionaCliente
andinsereCliente
are strange names for classes. It seems that the problem is different. What would be the responsibility of each class?– rray
the client class will validate and store the form information the insert class will do the Indsert in bdd and the selectClient will do the select in bdd and the two must take the already validated client class information
– Rodrigo Marques
@Rodrigomarques The answer solved your question? Do you think you can accept it? See [tour] if you don’t know how to do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points, and you will have to accept).
– Maniero