为什么我的方法没有输出


Why does my methods have no output?

我正在创建一个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。控制器.php

.class。GMAPI.php

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

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

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

此代码调用 setter

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

祝您编码愉快!

它们的方法

setFrom()setTo()不返回任何内容。 他们只设置一个变量。 即使回声也不起作用,因为没有什么可echo.