[英]PHP object cannot find method
So I have a very simple class that has a method called getThumbUrl() but when I try calling this method on an instance I get 所以我有一个非常简单的类,它具有一个名为getThumbUrl()的方法,但是当我尝试在实例上调用此方法时,我得到
Notice: Undefined property: FlickrImage::$getThumbUrl 注意:未定义的属性:FlickrImage :: $ getThumbUrl
But it is clearly there. 但这显然在那里。 Here is the code of the function inside of the FlickrImage class:
这是FlickrImage类内部函数的代码:
public function getThumbUrl()
{
return "http://farm".$this->_farm.".static.flickr.com/".$this->_server."/".$this->_id."_".$this->_secret."_t.jpg";
}
And here is where it fails inside of a different testing file: 这是在另一个测试文件中失败的地方:
$flickrTester = new FlickrManager();
$photos = $flickrTester->getPhotoStreamImages(9, 1);
foreach($photos as $photo) {
echo "<img src='$photo->getThumbUrl()' />";
}
Add curly-braces around the $photo->getThumbUrl()
in your echo. 在回显中的
$photo->getThumbUrl()
周围添加花括号。 Here's whats going on. 这是怎么回事。 Without surrounding the method in curly-braces PHP will try to resolve
$photo->getThumbUrl
and will treat the ()
as plain text. 在不使用大括号括
$photo->getThumbUrl
该方法的情况下,PHP将尝试解析$photo->getThumbUrl
,并将()
视为纯文本。 The error that you're seeing then is PHP complaining that $photo->getThumbUrl
hasn't been declared, as indeed it hasn't. 然后,您看到的错误是PHP抱怨
$photo->getThumbUrl
尚未声明,实际上没有声明。
foreach($photos as $photo) {
echo "<img src='{$photo->getThumbUrl()}' />";
}
From what you say, you are calling it like: 用您所说的话,您这样称呼它:
FlickrImage::$getFullUrl
Rather than: 而不是:
FlickrImage::$getFullUrl()
So you are missing ()
at the end. 因此,您最后缺少
()
。
foreach($photos as $photo) {
echo '<img src="' . $photo->getThumbUrl() . '" />';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.