How to reuse data in a class without having to repeat this data?

0

How to reuse $dados , $key and $postFields on all methods without having to repeat this data every time within each method? Yes, these variables will always have the same values.

class AllImoveis
{
    /**
     * Chaves de Autenticação
     * @var string
    */
    public function listar_imoveis()
    {
    $dados = array(fields =>array('tipo', 'cidade', 'bairro', 'codigo'));
    $key = '82CDA6l0BBepOykevP0472xl9ZoKuIlH';
    $postFields = json_encode($dados);  
    ...

    ...
    }
    public function listar_imovel()
    {
    $dados = array(fields =>array('tipo', 'cidade', 'bairro', 'codigo'));
    $key = '82CDA6l0BBepOykevP0472xl9ZoKuIlH';
    $postFields = json_encode($dados);  
    ...

    ...
    }
}
    
asked by anonymous 26.12.2015 / 13:46

2 answers

2

For $dados and $key make constants since the value will not change, $postFields will become an attribute of the class.

class AllImoveis {
  const KEY = '82CDA6l0BBepOykevP0472xl9ZoKuIlH';
  const DADOS = array(fields =>array('tipo', 'cidade', 'bairro', 'codigo'));
  private $postFields;
    
26.12.2015 / 14:27
1

so that the values are declare constants, const const_name, without the dollar sign. to use within the methods call the variable of this self mode :: CONSTANT_NAME is not required but it is recommended to use uppercase for constants.

    
26.12.2015 / 15:40