How to get the Return of a method that depends on another within a class?

Asked

Viewed 566 times

1

I’m using a class called Semelhantes that will have 2 methods.

The features magpie 3 characteristics of the current property through the id.

The resemblant rides a query which aims to bring properties that look like the current property and shows them as a kind of related properties.

You can see in the method features at the end of which I have already been able to store the attributes I will use to call the query through the method resemblant. These attributes are stored.

I am sending these attributes to the method resemblant but when trying to call the information as can be observed at the bottom of the code, instantiating the object and storing the result, I do not have the return of the method.

In this case, how to get the return of the method resemblant()?

<?php 

require("Acesso.class.php");

class Semelhantes extends Acesso
{
    public function features($id)
    {
        $postFields  = '{"fields":["Codigo","Categoria","Bairro","Cidade","ValorVenda","ValorLocacao","Dormitorios","Suites","Vagas","AreaTotal","AreaPrivativa","Caracteristicas","InfraEstrutura"]}';
        $url         = 'http://danielbo-rest.vistahost.com.br/'.$this->vsimoveis.'/'.$this->vsdetalhes.'?key=' . $this->vskey;
        $url           .= '&imovel='.$id.'&pesquisa=' . $postFields;

        $ch = curl_init($url);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $ch, CURLOPT_HTTPHEADER , array( 'Accept: application/json' ) );
        $result = curl_exec($ch); 
        $result = json_decode($result, true);

        /**
         * Paramentros para filtrar semelhança
         * @var [type]
         */
        $fcidade    = str_replace(" ", "+", $result['Cidade']);
        $fdorms     = $result['Dormitorios'];
        $fvalor     = $result['ValorVenda'];

        return array(
            'cidade' => $fcidade, 
            'dorms' => $fdorms, 
            'valor' => $fvalor
        );

    }

    public function resemblant()
    {
        $get = $this->features($id);
        return $get['Cidade'];
    }

}

/* Chamando as funções em outra parte do sistema */
$obj        = new Semelhantes;
$features   = $obj->features(2);
$similar    = $obj->resemblant();
  • @bigown the proof that I am studying is that I am already writing correctly, type, speaking classes, methods, attributes, objects instead of other variable type names which is not the case within a class :)

  • 2

    But it continues to do inheritance where there is no hierarchy :D Using the correct terms is good, but using the concepts correctly is absurdly more important. I don’t have time to see this but there are other strange things in the class. Besides, the class is not making the code any better right now. I hope after this happens, otherwise it’s just a waste of resources.

  • @bigown Assuming that I can use this module in various places of the system, even so it would not be interesting within a class where I rescue only the return?

  • I don’t think so, but I don’t have time to analyze.

  • by what I understand @Marcosvinicius you want to call resemblant() inside Features()? correct? you can use static methods if this is the problem. Thus the method would not be called by the class instance but by its name, which would make it possible to call a method without instantiation of the object.

  • I don’t understand the problem.

Show 1 more comment

1 answer

2


Simple Ué, simply store the id parameter in a class variable.

class Semelhantes extends Acesso
{
    private $id = null;
    public function features($id)
    {
        $this->id = $id;

and then in resemblant:

public function resemblant()
{
    $get = $this->features($this->id);
    return $get['Cidade'];
}

Browser other questions tagged

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