【问题标题】:How to return and print a specific method?如何返回并打印特定方法?
【发布时间】:2016-01-06 23:28:49
【问题描述】:

我在课堂上有两个方法。

第一种方法get$id通过webservice获取特定item的3个参数。这是一个房地产系统的网络服务。我将使用这 3 个参数来查找相关项目。

到目前为止,一切正常。

我的问题是返回resemblant()方法的数据。

我实例化了object,我将$id 发送到features 方法并注册了属性

$get 属性返回信息但$similar = $obj->resemblant() 没有返回。

我正在学习。

如何在$similar中返回方法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();

非常感谢

【问题讨论】:

  • var_dump($result) in features 是否已填充
  • 请不要在收到您的问题的有效答案后修改您的问题。它使答案看起来不必要和愚蠢。 SO 的主要目的是提供可搜索的问题和答案,以供其他人在遇到类似问题时使用
  • 好的@RiggsFolly,非常抱歉!!!
  • 您实例化了该对象并调用了features() 方法并使用$id 传递它,并且您希望返回的值被填充到resemblant() 方法中的$get 中,其中在该方法的范围内@ 987654337@ 未定义?
  • @Dagon var_dump 返回数据...puu.sh/mmemu/5679f4382a.png

标签: php arrays class methods instance


【解决方案1】:

您有多种选择来解决此处的问题

首先你可以改变resemblant()来接收参数

public function resemblant($id)
{
    $get = $this->features($id);
    return $get['cidade'];   // case of variable fixed also
}

并使用与features()相同的参数调用它

/* Chamando as funções em outra parte do sistema */
$obj        = new Semelhantes;
$features   = $obj->features(2);
$similar    = $obj->resemblant(2);
echo $similar;

或者您可以将传递给features()时的参数作为属性保存并在resemblant()中重复使用

protected $last_id;

public function features($id)
{

    $this->last_id = $id;

    // other existing code

}

public function resemblant()
{
    $get = $this->features($this->last_id);
    return $get['cidade'];   // case of variable fixed also
}

然后像原来那样调用这些方法

/* Chamando as funções em outra parte do sistema */
$obj        = new Semelhantes;
$features   = $obj->features(2);
$similar    = $obj->resemblant();

echo $similar;

【讨论】:

  • 这是一个非常好的解决方案@RiggsFolly ...感谢您对我的帮助和宽容。我不会说英语,但我会尝试。
【解决方案2】:

在第一个函数中,您可以这样设置数组:

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

在第二个函数中,您可以像这样访问值:

return $get['Cidade'];

注意到 cidadecidade 了吗?不同的情况。这就是你的意思:

return $get['cidade'];

您可以在此处阅读有关数组区分大小写的更多信息:PHP array, Are array indexes case sensitive?

【讨论】:

  • echo $similiar 会做到的
  • case sensitivity 似乎不是问题,但我更正了这个:)
  • 嗯,这是当时的问题之一。很高兴你明白了:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-03
  • 1970-01-01
  • 2014-09-28
  • 1970-01-01
  • 1970-01-01
  • 2014-01-03
相关资源
最近更新 更多