Values of attributes of a duplicated object

Asked

Viewed 88 times

0

I have the following problem, I have a class called Cliente with several attributes and a class called DAO_Cliente that returns a particular account (MySQL). The problem is when I try to put the values in the attributes of that object, ie when I $id and the $nome (only) for example all attributes are with value of the NOME, as if I set the attribute $telefone1 again all attributes were left with the value of TELEFONE. I’m using the php4.

Below is the class structure:

Class Cliente:

<?php
    include_once "endereco.php";

    class Cliente{
        public $id;
        public $nome;
        public $dataNasc;
        public $cpf;
        public $identidade;
        public $oEndereco;
        public $telefone1;
        public $telefone2;
        public $referencia;
        public $status;
        public $inscricaoEstadual;
        public $celular;
        public $email;
        public $dataVencimento;
        public $dataVisitaTecnica;
        public $valorMensalidade;
        public $regimeTributario;
        public $observacao;
        public $qtdeEcf;
        public $modelo;
        public $possuiNFe;

        function __construct() {
            $id = 0;
            $nome = '';
            $dataNasc = 0;
            $cpf = '';
            $identidade = '';
            $oEndereco = new Endereco();
            $telefone1 = '';
            $telefone2 = '';
            $referencia = '';
            $status = '';
            $inscricaoEstadual = '';
            $celular = '';
            $email = '';
            $dataVencimento = 0;
            $dataVisitaTecnica = 0;
            $valorMensalidade = 0;
            $regimeTributario = '';
            $observacao = '';
            $qtdeEcf = 0;
            $modelo = '';
            $possuiNFe = '';
        }
    }
?>

Class Dao_Cliente:

    <?php
    include_once "../conexao.php";
    include_once "../model/cliente.php";

    class DAO_Cliente{
        public function consultar($lcodCli){
            $lconexao = conexao::getInstance();
      $sql = "SELECT * FROM CLIENTES c WHERE c.codigo_cli = '".$lcodCli."'";
      $lconexao->$result = mysql_query($sql) or die("erro de consulta");        
      if ($consulta = mysql_fetch_array($lconexao->$result)){
        $auxCliente = new Cliente();
        $auxCliente->$id = $consulta[CODIGO_CLI];
        $auxCliente->$nome = $consulta[NOME];
        $auxCliente->$dataNasc = $consulta[NASCIMENTO];
        $auxCliente->$cpf = $consulta[CPF];
        $auxCliente->$identidade = $consulta[IDENTIDADE];
        $auxCliente->oEndereco->$logradouro = $consulta[ENDERECO];
        $auxCliente->oEndereco->$cep = $consulta[CEP];
        $auxCliente->oEndereco->$bairro = $consulta[BAIRRO];
        $auxCliente->oEndereco->$cidade = $consulta[CIDADE];
        $auxCliente->$telefone1 = $consulta[TELEFONE1];
        $auxCliente->$telefone2 = $consulta[TELEFONE2];
        $auxCliente->$referencia = $consulta[REFERENCIA];
        $auxCliente->$status = $consulta[STATUS];
        $auxCliente->$inscricaoEstadual = $consulta[Inscricao_Estadual];
        $auxCliente->$celular = $consulta[Celular];
        $auxCliente->$email = $consulta[Email];
        $auxCliente->$dataVencimento = $consulta[Dvencimento];
        $auxCliente->$dataVisitaTecnica = $consulta[DVisitaTecn];
        $auxCliente->$valorMensalidade = $consulta[Valor_da_mensalidade];
        $auxCliente->$regimeTributario = $consulta[Regime_tributario];
        $auxCliente->$observacao = $consulta[Observacao];
        $auxCliente->$qtdeEcf = $consulta[QUANT_ECFs_INST];
        $auxCliente->$modelo = $consulta[Modelo];
        $auxCliente->$possuiNFe = $consulta[NFe];

        mysql_free_result($lconexao->$result); 

                return $auxCliente;
      }

      return null;

    }
?>

And here’s the call where attribute values are duplicated:

if ($auxCliente != null){
      echo $auxCliente->$id." - ".$auxCliente->$nome." - ".$auxCliente->$dataNasc." - ".$auxCliente->$cpf." - ".$auxCliente->$identidade." - ".$auxCliente->oEndereco->$logradouro." - ".$auxCliente->oEndereco->$bairro." - ".$auxCliente->oEndereco->$cep." - ".$auxCliente->oEndereco->$cidade." - ".$auxCliente->$telefone1." - ".$auxCliente->$telefone2." - ".$auxCliente->$referencia." - ".$auxCliente->$status." - ".$auxCliente->$inscricaoEstadual." - ".$auxCliente->$celular." - ".$auxCliente->$email." - ".$auxCliente->$dataVencimento." - ".$auxCliente->$dataVisitaTecnica." - ".$auxCliente->$valorMensalidade." - ".$auxCliente->$regimeTributario." - ".$auxCliente->$observacao." - ".$auxCliente->$qtdeEcf." - ".$auxCliente->$modelo." - ".$auxCliente->$possuiNFe;
    }

1 answer

2


I suggest you add simple quotes on all accesses of the database array and also assign values in the client object properties.

Change the occurrences of:

$auxCliente->$id = $consulta[CODIGO_CLI];

To

$auxCliente->id = $consulta['CODIGO_CLI'];

------------^               ^----------^
cifrão removido             aspas adicionadas

When doing a partial test the following errors/warnings were returned:

Undefined variable: id in

and

Cannot access Empty Property in

phpfiddle - example of errors.

The same goes for starting the attributes in the constructor, the way you are basically creating a lot of variables that lost value after the constructor is invoked.

Change:

function __construct() {
   $id = 0;

To:

function __construct() {
   $this->id = 0;
  • I removed the simple quotes from the database array accesses just to test, but with or without quotation marks the error persists...

  • @Robson Removed the dollar?

  • Yes, as you said, only now the attributes of the Client object no longer have any value! In the object Address (within Client) the values of the street attribute are duplicated! Very strange

  • Well, what I realized is that if I take the dollar sign the values of the kind attributes they don’t attribute, they lose the values...

  • Which php vesão you are using?

  • 1

    Unfortunately the version 4

  • Aaaaa so... I’ll change the answer, she’s facing php5.

  • Worked now?

  • 1

    No need! It’s already solved! Thank you very much rray. The problem was precisely in the dollar sign, both in the $auxCliente->id = $query['CODIGO_CLI']; and in the $auxCliente->id call. As I’m new in php I think I ended up confusing myself and calling always $attribute

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.