为什么我的方法没有输出?

时间:2014-08-18 20:16:53

标签: php class

我正在创建一个GoogleMapApi类。在网上找到一些,但我认为它们对我不是很好。所以我决定建立自己的。

实际上那不是我的第一堂课而且我已经习惯了......

这是我得到的:

class GMAPI{

    private $apiKey;
    private $from;;

    public function GMAPI(){

        $this->apiKey = GOOGLE_API_KEY;
    }

    public function setFrom($string){

        $this->from = $this->prepareString($string);
    }


    public function getDistance($type="meter"){

        echo $this->from; //output is empty
        echo $type;       //outputs: meter
    }
}

现在我有一个包含此内容的其他php文件

$gmapi = new GMAPI();
$gmapi->setFrom('New York');
$gmapi->getDistance();

//output: meter
//expacted output: New Yorkmeter

我也试过这个

public function setFrom($string){
    echo $string;
    $this->from = $string;
}

但仍然没有结果。甚至没有

public function setFrom($string="foobar"){
    echo $string;
    $this->from = $string;
}

我做错了什么?!?!?

编辑:将我的帖子缩短以获得更好的概述

完整代码

class.Controller.php

class.GMAPI.php

(根据一些评论,我将一些var更改为没有使用保留的var)

2 个答案:

答案 0 :(得分:2)

此代码动态创建新属性,因此只需将其更改为下面的示例

$this->gmapi->setFromAddress   =   "New York";
$this->gmapi->setToAddress     =   "Boston"; 

此代码调用setters

$this->gmapi->setFromAddress("New York");
$this->gmapi->setToAddress("Boston"); 

快乐的编码!

答案 1 :(得分:-1)

他们的方法setFrom()setTo()不会返回任何内容。他们只设置一个变量。即使是回音也行不通,因为echo没有任何内容。

相关问题