[英]Why does my methods have no output?
I am creating a GoogleMapApi class. 我正在创建一个GoogleMapApi类。 Found some in the net but I thought they are not very good for me.
在网上找到了一些东西,但我认为它们对我不是很好。 So I decided to build my own.
所以我决定建立自己的。
Actually thats not my first class and I am used to it.... 其实那不是我的第一堂课,我已经习惯了...。
Here is what I got: 这是我得到的:
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
}
}
now I have a other php file with this content 现在我有与此内容的另一个php文件
$gmapi = new GMAPI();
$gmapi->setFrom('New York');
$gmapi->getDistance();
//output: meter
//expacted output: New Yorkmeter
i have also tried this 我也尝试过
public function setFrom($string){
echo $string;
$this->from = $string;
}
But still no result. 但是仍然没有结果。 Not even with
甚至没有
public function setFrom($string="foobar"){
echo $string;
$this->from = $string;
}
What am I doing wrong ?!?!? 我究竟做错了什么 ?!?!?
EDIT: Shorted my Post to have a better overview 编辑: 缩短我的帖子,以得到更好的概述
class.Controller.php class.Controller.php
class.GMAPI.php class.GMAPI.php
(according to some comments i changed some var's to not have reserved var's in use) (根据一些评论,我将某些变量更改为未使用保留变量)
this code creates new properties on the fly, so it just should be changed as an example below 这段代码可以动态创建新属性,因此,仅在下面的示例中进行更改
$this->gmapi->setFromAddress = "New York";
$this->gmapi->setToAddress = "Boston";
this code calls setters 该代码调用设置器
$this->gmapi->setFromAddress("New York");
$this->gmapi->setToAddress("Boston");
Happy coding! 编码愉快!
them methods setFrom()
and setTo()
don't return anything. 它们的方法
setFrom()
和setTo()
不返回任何东西。 they only set a variable. 他们只设置一个变量。 even echoing won't work because there is nothing to
echo
. 甚至回声都行不通,因为没有
echo
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.